ホームページ > php教程 > PHP开发 > コンパクトなクラス ソリューション

コンパクトなクラス ソリューション

高洛峰
リリース: 2016-11-30 17:01:45
オリジナル
1302 人が閲覧しました

1

2

3

4

5

6

7

8

9

10

11

12

13

14

(function(){

    function Extend(func,proto){

        func.prototype.__proto__=proto.prototype;

        Object.defineProperty(func.prototype,"proto",{

            value: proto.prototype

        });

    }

    function Super(func,method){

        if(!method) method='constructor';

        return func.prototype.__proto__[method];

    }

    window.Extend=Extend;

    window.Super=Super;

})();

ログイン後にコピー

スーパーのスーパーを処理中に無限ループが発生しました:

this.super-->this.proto.constructor(){this.super}-->this.proto.constructor。 。 。

その後、あまり複雑にしたくなかったので、上記のコードのメソッドを直接使用しました(知らなかっただけです...)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

(function(){

    function AAA(name){

        this.name=name;

    }

    function BBB(name){

        Super(BBB).call(this,name);

    }

    Extend(BBB,AAA);

    function CCC(name,age){

        Super(CCC).call(this,name);

        this.age=age;

    }

    Extend(CCC,BBB);

    var c=new CCC('ccc',18);

    console.log(c);

})();

ログイン後にコピー

その後、関数を汚したくなかったので、窓を汚すことしかできませんでした。 。 。

関数の中に入れた方が簡単でしょうか?

りー


関連ラベル:
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のおすすめ
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート