I have such a problem. I need to change the property of an override-protected object Object.defineProperty({configurable: true}) . I'm trying to do this through inheritance, which of course is not in JavaScript. To do this, I take the functions that are proposed for such tasks by the JavaScript: extend(Child, Parent) и mixin(dst, src) gurus JavaScript: extend(Child, Parent) и mixin(dst, src)
function extend(Child, Parent) { var F = function() { } F.prototype = Parent.prototype Child.prototype = new F() Child.prototype.constructor = Child Child.superclass = Parent.prototype } // копирует все свойства из src в dst, // включая те, что в цепочке прототипов src до Object function mixin(dst, src){ // tobj - вспомогательный объект для фильтрации свойств, // которые есть у объекта Object и его прототипа var tobj = {} for(var x in src){ // копируем в dst свойства src, кроме тех, которые унаследованы от Object if((typeof tobj[x] == "undefined") || (tobj[x] != src[x])){ try { dst[x] = src[x]; } catch (e) { console.error("Error property: " + x); } } } // В IE пользовательский метод toString отсутствует в for..in if(document.all && !document.isOpera){ var p = src.toString; if(typeof p == "function" && p != dst.toString && p != tobj.toString && p != "\nfunction toString() {\n [native code]\n}\n"){ dst.toString = src.toString; } } } // Мой код var myKeyboardEvent = function() { this.getLastElement = function() { return this[this.length-1]; } } extend(myKeyboardEvent, KeyboardEvent); //elem может быть любое поле <input type="text" name="elem" id="elem"> function pressKey(elem){ var evt = new myKeyboardEvent("keypress"); mixin(myKeyboardEvent.prototype, new KeyboardEvent("keypress")); elem.dispatchEvent(evt); } pressKey(elem); <input type="text" name="elem" id="elem"> My code crashes by dispatchEvent :
Uncaught TypeError: Failed to execute 'dispatchEvent' on 'EventTarget': parameter 1 is not of type 'Event'.
I need to override the isTrusted property in the object returned by the KeyboardEvent .
How can anything properly inherit embedded JavaScript objects?