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

发布于 2024-08-10 02:18:23 字数 362 浏览 12 评论 0原文

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();
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();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

远山浅 2024-08-17 02:18:23

JavaScript 函数是对象,有两个有用的方法来调用该函数:

Function.call(scope, [arg1, ...])
Function.apply(scope, args)

您可以使用其中之一来调用父实现,显式传递 this 作为 scope 参数,以便在父实现中,this 引用子对象:

var o1 = {
    name : "One",
    init : function() {
        alert("o1: " + this.name);
    }
};

var o2 = Object.create(o1);
o2.name = "Two";
o2.init = function() {
    o1.init.call(this);
    alert("o2: " + this name);
};

这将发出警报:o1: Twoo2: Two

JavaScript functions are objects and have two useful methods to invoke the function:

Function.call(scope, [arg1, ...])
Function.apply(scope, args)

You can use one of these to call the parent implementation, explicitely passing this as the scope parameter, so that in the parent implementation, this refers to the child object:

var o1 = {
    name : "One",
    init : function() {
        alert("o1: " + this.name);
    }
};

var o2 = Object.create(o1);
o2.name = "Two";
o2.init = function() {
    o1.init.call(this);
    alert("o2: " + this name);
};

This will alert: o1: Two and o2: Two.

千鲤 2024-08-17 02:18:23

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

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

出于好奇,“ancessors”是“ancestor's”的拼写错误,还是“ancessors”在这里有特定的含义?您是指 o2 的“父”对象吗?

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?

鹿! 2024-08-17 02:18:23

在支持它的浏览器中,您可以使用 Object.getPrototypeOf 函数,如下所示:

o2.init = function(){
    Object.getPrototypeOf(this).init.call(this);
    alert('o2');
};

这将获取 o2 (o1) 的原型,并且将其 init 方法应用于此 (o2),就像其他语言中的 super.init() 一样。

更新:

Object.getPrototypeOf 函数可以这样实现:

if ( typeof Object.getPrototypeOf !== "function" ) 
{
    if ( typeof ({}).__proto__ === "object" ) 
    {
        Object.getPrototypeOf = function(object)
        {
            return object.__proto__;
        };
    } 
    else 
    {
        Object.getPrototypeOf = function(object)
        {
            // May break if the constructor has been tampered with
            return object.constructor.prototype;
        };
    }
}

在此链接上找到:http ://ejohn.org/blog/objectgetprototypeof/

In browsers that support it, you could use the Object.getPrototypeOf function, like this:

o2.init = function(){
    Object.getPrototypeOf(this).init.call(this);
    alert('o2');
};

This would get the prototype of o2 (o1) and apply its init method to this (o2), just like a super.init() in other languages.

UPDATE:

The Object.getPrototypeOf function could be implemented like this:

if ( typeof Object.getPrototypeOf !== "function" ) 
{
    if ( typeof ({}).__proto__ === "object" ) 
    {
        Object.getPrototypeOf = function(object)
        {
            return object.__proto__;
        };
    } 
    else 
    {
        Object.getPrototypeOf = function(object)
        {
            // May break if the constructor has been tampered with
            return object.constructor.prototype;
        };
    }
}

Found on this link: http://ejohn.org/blog/objectgetprototypeof/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文