0

これが私の継承を達成するための私のコードです:

<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>

継承された親クラスの「親」を表示するには、子console.logが必要ですが、現時点では、未定義のみが表示されます。

親プロパティから継承しない理由がわかりません。

よろしくお願いします。

4

1 に答える 1

0

Parentプロパティを設定している関数を呼び出すことは決してないからです。inherit関数をよく見ると、のprototypeプロパティを使用してPいますが、呼び出すことはありません。P これは良いことです。それはChild誰が呼ぶべきかPです:

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

明らかに、これには、への呼び出しとParentの両方で、複数の場所にリストする必要があるという欠点があります。次の子関数に親コンストラクターを設定することで、これを軽減できます。inheritChildinherit

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

...したがって、次のようにChildなります。

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

(「スーパー」メソッドの呼び出しなどをサポートして)階層を完全に実行することに興味がある場合は、私のLineageツールキットを確認することをお勧めします。これは、このようなものの多くを自動化する小さなツールキットですが、このようなものがどのように機能するかを知りたい場合は、ソースが興味深い読み物になる可能性があります。そこには、使用せずに多層で完全に機能する継承を示すwikiページもあります(使用比較する手段として)。LineageLineage

于 2012-05-12T09:38:10.583 に答える