JS類比類繼承

來源:互聯網
上載者:User

標籤:

    //最後一個參數是JSON表示的類定義

    //如果參數大於一,則第一個參數是基類,否則,基類是object

    //中間的參數是類實現的介面

    //傳回值是類,類是一個建構函式,並繼承了基類的prototype


    

function Class(){

        aDefine = arguments[arguments.length - 1]; //傳入的最後一個參數是要定義的類

        if(!aDefine) return; //如果沒用傳參數,則直接結束。

        var aBase = arguments.length>1?arguments[0]:object; //如果傳的參數大於1,則第一個參數為基類,若等於1,則基類預設為object

        function prototype_(){}; //建立prototype臨時函數,用以掛接原型鏈

        prototype_.prototype = aBase.prototype;

        var aPrototype = new prototype_(); //建立類要使用的原型對象

        for(var member in aDefine){

            if(aDefine[member] != ‘Create‘){ //建構函式不用掛在原型鏈上,建構函式是最終返回的

                aPrototype[member] = aDefine[member];

            }

        }

        //設定類

        if(aDefine.Create){  //如果有建構函式

            var aType = aDefine.Create; //類即為該建構函式

        }else{ //如果沒有建構函式

            var aType = function(){ //建立預設的建構函式

                this.base.apply(this, arguments); //調用父類的建構函式

            }

        }

        aType.prototype = aPrototype;  //設定類(建構函式)的原型

        aType.Base = aBase; //設定類型關係

        aType.prototype.Type = aType; //為本類對象擴充一個Type屬性,用以判斷對象的類型

        return aType; //將加了原型繼承的建構函式返回,就可以new了

    }

    function object(){}; //定義object根類,用以實現最基礎的方法和屬性

    object.prototype.isA = function(type){ //判斷對象屬性是否屬於某種類型

        var self = this.Type;


        while(self){

            if(self == type){

                return true;

            }

            self = self.Base;

        }

        return false;

    }

    object.prototype.base = function(){   //調用基類的建構函式

        var Caller = object.prototype.base.caller; //得到調用該函數的函數

        Caller && Caller.Base && Caller.Base.apply(this, arguments);

        //如果Caller存在並且 Caller.Base存在,則調用 Caller.Base方法

        //若果如果Caller或Caller.Base有一個不存在,那麼後面的語句不用再執行。

    }

    var people = Class({ //定義people類

        Create: function(name, age){

            this.base();

            this.name = name;

            this.age = age;

        },

        say: function(){

            console.log(‘My name is ‘ + this.name + ",I‘m " + this.age + ‘.‘);

        }

    })

    var chinaPeople = Class(people,

        {

            Create: function(name, age, country){

                this.base(name, age);

                this.country = country;

            },

            sayCountry: function(){

                console.log(‘My country is ‘ + this.country);

            }

        }

    )

    var man1 = new people(‘kaka‘, 32);

    var man2 = new chinaPeople(‘kaka‘, 32, ‘china‘);

    man2.say();  //My name is kaka,I‘m 32.

    man2.sayCountry();  //My country is china 




caller不支援opera瀏覽器,所以重寫base方法

  object.prototype.base = function(){

        var Base = this.Type.Base; //擷取當前對象的基類

        if(!Base.Base){ //如果已經沒有基類

            Base.apply(this, arguments); //直接調用基類建構函式

        }else{

            this.base = MakeBase(Base); //先複寫this.base

            Base.apply(this, arguments);

            delete this.base; //刪除複寫的base屬性

        };

        function MakeBase(Type){

            var Base = Type.Base;

            if(!Type.Base) return Base //基類已無基類,就無需封裝

            return function(){ //封裝為引用臨時變數Base的閉包函數

                this.base = MakeBase(Base); //先複寫this.base

                Base.apply(this, arguments); //在調用基類建構函式

            }

        }

    }


JS類比類繼承

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.