<tfoot id='yrndQ'></tfoot>

  1. <i id='yrndQ'><tr id='yrndQ'><dt id='yrndQ'><q id='yrndQ'><span id='yrndQ'><b id='yrndQ'><form id='yrndQ'><ins id='yrndQ'></ins><ul id='yrndQ'></ul><sub id='yrndQ'></sub></form><legend id='yrndQ'></legend><bdo id='yrndQ'><pre id='yrndQ'><center id='yrndQ'></center></pre></bdo></b><th id='yrndQ'></th></span></q></dt></tr></i><div id='yrndQ'><tfoot id='yrndQ'></tfoot><dl id='yrndQ'><fieldset id='yrndQ'></fieldset></dl></div>

    1. <small id='yrndQ'></small><noframes id='yrndQ'>

      • <bdo id='yrndQ'></bdo><ul id='yrndQ'></ul>
    2. <legend id='yrndQ'><style id='yrndQ'><dir id='yrndQ'><q id='yrndQ'></q></dir></style></legend>

      JavaScript 继承和超级构造函数

      JavaScript inheritance and super constructor(JavaScript 继承和超级构造函数)
      <legend id='SgkF2'><style id='SgkF2'><dir id='SgkF2'><q id='SgkF2'></q></dir></style></legend>

        <i id='SgkF2'><tr id='SgkF2'><dt id='SgkF2'><q id='SgkF2'><span id='SgkF2'><b id='SgkF2'><form id='SgkF2'><ins id='SgkF2'></ins><ul id='SgkF2'></ul><sub id='SgkF2'></sub></form><legend id='SgkF2'></legend><bdo id='SgkF2'><pre id='SgkF2'><center id='SgkF2'></center></pre></bdo></b><th id='SgkF2'></th></span></q></dt></tr></i><div id='SgkF2'><tfoot id='SgkF2'></tfoot><dl id='SgkF2'><fieldset id='SgkF2'></fieldset></dl></div>

            <tbody id='SgkF2'></tbody>
            <bdo id='SgkF2'></bdo><ul id='SgkF2'></ul>
              <tfoot id='SgkF2'></tfoot>
            1. <small id='SgkF2'></small><noframes id='SgkF2'>

                本文介绍了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;
                    };
                
                })();
                

                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. You can access super properties via this.__super__.propertyName instead of ClassName.__super__.propertyName. So you have no redundancy in the class naming.

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

                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?

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

                解决方案

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

                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, …)
                

                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 because you did not get the .__super__ that you expected!

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

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
                quot;Each child in an array should have a unique key propquot; only on first time render of page(“数组中的每个孩子都应该有一个唯一的 key prop仅在第一次呈现页面时)
                Rails 3.1 ajax:success handling(Rails 3.1 ajax:成功处理)
                CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
                Ordinals in words javascript(javascript中的序数)
                getFullYear returns year before on first day of year(getFullYear 在一年的第一天返回前一年)
                  <tbody id='8tepj'></tbody>

                    <small id='8tepj'></small><noframes id='8tepj'>

                    <i id='8tepj'><tr id='8tepj'><dt id='8tepj'><q id='8tepj'><span id='8tepj'><b id='8tepj'><form id='8tepj'><ins id='8tepj'></ins><ul id='8tepj'></ul><sub id='8tepj'></sub></form><legend id='8tepj'></legend><bdo id='8tepj'><pre id='8tepj'><center id='8tepj'></center></pre></bdo></b><th id='8tepj'></th></span></q></dt></tr></i><div id='8tepj'><tfoot id='8tepj'></tfoot><dl id='8tepj'><fieldset id='8tepj'></fieldset></dl></div>

                        <bdo id='8tepj'></bdo><ul id='8tepj'></ul>
                      • <legend id='8tepj'><style id='8tepj'><dir id='8tepj'><q id='8tepj'></q></dir></style></legend><tfoot id='8tepj'></tfoot>