JavaScript继承和超级构造函数 [英] JavaScript inheritance and super constructor

查看:84
本文介绍了JavaScript继承和超级构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现并修改了一个来自coffeescript的JavaScript类扩展函数:

I have found and adapted a JavaScript "class" extend function from coffeescript:

var extend = (function() {

    var hasProp = Object.prototype.hasOwnProperty;

    function ctor(child) {
        this.constructor = child;
    }

    return function(child, parent) {
        for (var key in parent) {
            if (hasProp.call(parent, key)) {
                child[key] = parent[key];
            }
        }
        ctor.prototype = parent.prototype;
        child.prototype = new ctor(child);
        child.__super__ = parent.prototype;
        // child.prototype.__super__ = parent.prototype; // better?
        return child;
    };

})();

我想知道,如果有理由为什么他们使用 child .__ super__ 而不是 child.prototype .__ super __ (请参阅注释代码行)。

I am wondering, if there is a reason why they used child.__super__ instead of child.prototype.__super__ (see out-commented code line).

我喜欢的评论版本更多因为:

I like the out-commented version more because:


  1. 您可以通过 __super __。propertyName 而不是 ClassName .__ super __。propertyName

这使得嵌套继承更有意义,因为您可以使用 this .__ super__。 __super __。propertyName 而不是 ClassName .__ super __。constructor .__ super __。propertyName

This makes even more sense for nested inheritance since you can use this.__super__.__super__.propertyName instead of ClassName.__super__.constructor.__super__.propertyName

我没有看到任何原因,但是你仍然可以用静​​态方式调用static函数:

I do not see any reason for it, but you could even still call "static" functions in a "static" way like that:

ClassName.prototype.__super__.constructor.staticMethod()

我的版本有没有我可能会忽略的缺点?

Are there any drawbacks with my version that I might have overlooked?

编辑:我更正了 var hasProp = Object.prototype .hasOwnProperty; c>

I corrected the line to var hasProp = Object.prototype.hasOwnProperty;

推荐答案

因为你不应该使用 __ super__

Because you're not supposed to use __super__ in your code at all.

这是一个编译器工件,每次使用 super 宏/关键字/无论是什么将编译为

It's a compiler artifact, every use of the super macro/keyword/whatever it is will compile to

ClassName.__super__.methodName.call(this, …) // or
ClassName.__super__.methodName.apply(this, …)
// or, in static class functions even
ClassName.__super___.constructor.functionName.call(this, …)

他们不信任动态<$ c $ ( this .__ super __ ),这些 绑定 。实际上,它可能是一个好主意,不使用一个属性,而只是一个本地超级变量在它们的模块范围。

They don't trust the dynamic this binding that you have proposed to use (this.__super__), they rather went for a static reference of the parent. Actually it might have been a good idea not to use a property at all, but just a local super variable in their module scope.

此外,此.__超级__ 将无法在继承的方法中使用:

Also, this.__super__ will not work in an inherited method:

function A() { }
A.prototype.method = function() { console.log("works") };

function B() { A.call(this); }
B.prototype = Object.create(A.prototype);
B.prototype.__super__ = A.prototype;
B.prototype.method = function() { this.__super__.method.call(this); }


function C() { B.call(this); }
C.prototype = Object.create(B.prototype);
C.prototype.__super__ = B.prototype;

var b = new B(), c = new C();
b.method() // "works"
c.method() // Maximum recursion depth exceeded

Stack Overflow,因为你没有得到你所期望的 .__ super __

Stack Overflow because you did not get the .__super__ that you expected!

这篇关于JavaScript继承和超级构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆