When an object is instantiated, be it a string/function/etc, a __proto__
property is included. This property seems to be generated by the __proto__
accessors in Object.prototype
...
Object.prototype == {
__defineGetter__ : __defineGetter__()
__defineSetter__ : __defineSetter__()
__lookupGetter__ : __lookupGetter__()
__lookupSetter__ : __lookupSetter__()
constructor : Object()
hasOwnProperty : hasOwnProperty()
isPrototypeOf : isPrototypeOf()
propertyIsEnumerable: propertyIsEnumerable()
toLocaleString : toLocaleString()
toString : toString()
valueOf : valueOf()
get __proto__ : __proto__() // getter
set __proto__ : __proto__() // setter
};
I'm wondering if it is possible to hijack this __proto__
property to execute a code block when an object is instantiated. The idea being to replace the __proto__
property with a custom property that executes some code before calling the original accessors to create the __proto__
on the new instance.
If that makes sense! If not here's where I'm up to:
pro = Object.prototype;
tmp = {};
Object.defineProperty(tmp, '__proto__',
Object.getOwnPropertyDescriptor(pro, '__proto__')
);
delete pro.__proto__;
Object.defineProperty(pro, '__proto__',{
get:function(){
console.warn('intercepted Get __proto__');
return tmp.__proto__;
},
set(p){
console.warn('intercepted Set __proto__');
tmp.__proto__ = p;
}
});
Can't tell if it works properly yet but it's only an example to try and show you what I'm trying to achieve.