How to access properties from the prototype chain that are obscured by their own properties?

Create an object that inherits from another anonymous object:

var obj = Object.create({ func: function () { alert('Inherited method'); } }); 

Now obj inherits the func method from this anonymous object (the obj prototype link points to this anonymous object).

 obj.func(); // alerts 'Inherited method' 

But if we assign the func property to obj itself, the inherited func property will be obscured :

 obj.func = function () { alert('Own method'); }; obj.func(); // alerts 'Own method' 

Live demo: http://jsfiddle.net/PLxHB/

Now, if we want to call this shadow func method (the one that warns the 'Inherited method' ), what would be a good way to do this?

I already found one solution - see here - but it's kind of a hack.

+4
source share
1 answer
 Object.getPrototypeOf(obj).func(); 

will ensure that the inherited function is executed.

In older browsers (above ES5) you can use

 obj.__proto__.func(); 

but it is out of date.

http://jsfiddle.net/pimvdb/PLxHB/5/

+3
source

All Articles