简体   繁体   English

Javascript继承:未启用父级属性

[英]Javascript inheritance : parent property not enabled

Here is my code to achieve my inheritance: 这是实现继承的代码:

<html lang="en">
<head>
    <title>JavaScript Patterns</title>
    <meta charset="utf-8">
</head>
<body>
    <script>
        /* Title: Classical Pattern #5 - A Temporary Constructor (a pattern that should be generally avoided)
         Description: first borrow the constructor and then also set the child's prototype to point to a new instance of the constructor
         */


        /* Basic */
        /*function inherit(C, P) {
         var F = function () {};
         F.prototype = P.prototype;
         C.prototype = new F();
         }*/


        /* Storing the Superclass */
        /*function inherit(C, P) {
         var F = function () {};
         F.prototype = P.prototype;
         C.prototype = new F();
         C.uber = P.prototype;
         }*/


        /* Resetting the Constructor Pointer */
        /*function inherit(C, P) {
         var F = function () {};
         F.prototype = P.prototype;
         C.prototype = new F();
         C.uber = P.prototype;
         C.prototype.constructor = C;
         }*/


        /* in closure */
        var inherit = (function () {
            var F = function () {
            };
            return function (C, P) {
                F.prototype = P.prototype;
                C.prototype = new F();
                C.uber = P.prototype;
                C.prototype.constructor = C;
            }
        }());


        function Parent(name) {
            this.nameParent = name || 'Adam';
            this.parentName = "parent";//this.nameParent;
        }


        // adding functionality to the prototype
        Parent.prototype.say = function () {
            return this.nameParent;
        };



        // child constructor
        function Child(nameChild) {
            console.log("parentprop:" + this.parentName);
        }

        inherit(Child, Parent);


        var kid = new Child();
        console.log(kid.name); // undefined
        console.log(typeof kid.say); // function
        kid.nameParent = 'Patrick';
        console.log(kid.parentName);
        console.log(kid.say()); // Patrick
        console.log(kid.constructor.nameParent); // Child
        console.log(kid.constructor === Parent); // false




        // reference
        // http://shop.oreilly.com/product/9780596806767.do
    </script>
</body>

I need the Child console.log to display "parent" from the parent class inherited, but for now, it only displays undefined. 我需要Child console.log来显示继承自父类的“父”,但是目前,它仅显示未定义。

I don't know why it doesn't inherit from parent property. 我不知道为什么它不继承自父属性。

Thanks in advance for your help. 在此先感谢您的帮助。

Because you never call the function Parent , which is what's setting the property. 因为您永远不会调用函数Parent ,这是设置属性的原因。 If you look closely at your inherit function, you're using the prototype property of P but you're never calling P — which is a good thing; 如果仔细查看inherit函数,您将使用Pprototype属性,但您永远不会调用P -这是一件好事。 it's Child who should call P : Child ,应该给P打电话:

function Child(nameChild) {
    Parent.call(this);
    console.log("parentprop:" + this.parentName);
}

Obviously, this has the disadvantage that you have to list Parent in more than one place — both in your call to inherit , and in Child . 显然,这样做的缺点是您必须在多个地方列出Parent ,无论是在inherit调用中还是在Child You could mitigate that by setting the parent constructor on the child function in inherit : 您可以通过在inherit中的inherit上设置父构造函数来减轻这种情况:

var inherit = (function () {
    var F = function () {
    };
    return function (C, P) {
        F.prototype = P.prototype;
        C.parent = P;          // <==== The new bit
        C.prototype = new F();
        C.uber = P.prototype;
        C.prototype.constructor = C;
    }
}());

...so then Child becomes: ...因此Child变为:

function Child(nameChild) {
    Child.parent.call(this);
    console.log("parentprop:" + this.parentName);
}

If you're interested in doing hierarchies thoroughly (with support for calling "super" methods and such), you might want to look at my Lineage toolkit . 如果您有兴趣彻底进行层次结构(支持调用“ super”方法等),则可能需要查看我的Lineage工具箱 It's a small toolkit that automates a lot of this stuff, but if you want to learni how this stuff works, the source might make for interesting reading. 这是一个小型工具箱,可自动执行很多此类工作,但是,如果您想学习这些工作原理,则可能会为您提供有趣的阅读源。 There's also a wiki page there showing multi-layer, fully-functional inheritance without using Lineage (as a means of comparing it with using Lineage ). 还有一个Wiki页面,其中显示了使用Lineage的多层,全功能继承(作为与Lineage进行比较的一种方式)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM