Search code examples
javascriptinheritanceprototypeobject-create

How to get to my ancestor’s overridden method using Crockfords's Object.create() (Javascript)


if (typeof Object.create !== 'function') {
    Object.create = function (o) {
        function F() {}
        F.prototype = o;
        return new F();
    };
}
var o1 = {};
o1.init = function(){
   alert('o1');
};
var o2 = Object.create(o1);
o2.init = function(){
   // how would I call my ancessors init()?
   alert('o2');
};
o2.init();

Solution

  • Maybe this is oversimplifying what you’re trying to accomplish ... would placing o1.init() in the o2 init function work?

    o2.init = function(){
       // how would I call my ancessors init()?
       alert('o2');
       o1.init();
    };
    

    Out of curiosity, was "ancessors" a spelling error for "ancestor’s" or does "ancessors" mean something specific here? Did you mean o2’s "parent" object?