如何使用 Crockfords 的 Object.create() (Javascript) 访问我祖先的重写方法

Jan*_*Jan 5 javascript inheritance prototype object-create

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();
Run Code Online (Sandbox Code Playgroud)

dan*_*_nl 2

也许这过于简单化了您\xe2\x80\x99想要完成的任务...将 o1.init() 放在 o2 init 函数中会起作用吗?

\n\n
o2.init = function(){\n   // how would I call my ancessors init()?\n   alert(\'o2\');\n   o1.init();\n};\n
Run Code Online (Sandbox Code Playgroud)\n\n

出于好奇,“ancessors”是“ancestor\xe2\x80\x99s”的拼写错误,还是“ancessors”在这里有特定的含义?您的意思是 o2\xe2\x80\x99s“父”对象吗?

\n