JS設計模式——4.繼承(概念)

來源:互聯網
上載者:User

類式繼承0.建構函式一個簡單的Person類 function Person(name){    this.name = name;}Person.prototype.getName = function(){    return this.name;}1.原型鏈實現繼承未封裝extend函數繼承繼承步驟如下: step 1:建立繼承類的建構函式 step 2:設定繼承類的原型鏈為父類建構函式。 step 3:調整繼承類的建構函式。 step 4:建立繼承類 複製代碼function Author(name, books){    Person.call(this, name); //step 1    this.books = books;}Author.prototype = new Person(); //step 2Author.prototype.constructor = Author; // step 3Author.prototype.getBooks = function(){    return this.books;};var author = new Author('ysy',['js desgin']); // step 4複製代碼簡單封裝extend函數繼承function extend(sub, super){    var F = function(){};    F.prototype = superClass.prototype;    sub.prototype = new F(); // step 2    sub.prototype.constructor = sub; // step3}這種展示了封裝的原理,但是step1並沒有封裝進來,而最重要的是step1中會產生對Person的耦合。 進階封裝extend函數繼承複製代碼function extend(sub, super){    var F = function(){};    F.prototype = superClass.prototype;    sub.prototype = new F(); // step 2    sub.prototype.constructor = sub; // step3    sub.super = super.prototype; // step 1    if(super.prototype.constructor == Object.prototype.constructor){        super.prototype.constructor = super;    }}複製代碼這樣在構建子類的建構函式時樣本如下: function Author(name, books){    Author.superClass.constructor.call(this, name);    this.books = books;}extend(Author, Person);原型式繼承 0.原型對象直接定義一個對象即可 var Person = {    name: 'ysy',    getName: function(){        return this.name;    }}  1.對繼承而來的成員的讀和寫的不對等性這個其實很好理解,如果在讀屬性之前還未對繼承而來的屬性進行賦值操作,那麼讀的就是super中的屬性,而此時寫的話,卻是對sub中的寫。 當然,一旦寫過之後,讀和寫就是對等的了。 這個簡單問題就不再給出代碼了。如果你不懂,就去徹底瞭解一下原型鏈。 2.clone函數function clone(obj){    function F(){}    F.prototype = obj;    return new F;}  3.繼承的實現var Author = clone(Person);Author.books = [];Author.getBooks = function(){    return this.books;}  貌似這個繼承的實現要簡單好多哇。 繼承與封裝只有公用和特權成員會被繼承下來。 因此,門戶大開型式最適合派生自雷的。它們的所有成員都是公開的,因此可以被遺傳給子類。如果某個成員需要稍加隱藏,可以用底線規範。 在派生具有真正的私人成員的類時,特權方法是公用的,可以被繼承,因此也就可以訪問父類的私人成員,但是我們不能在子類中添加能夠直接存取他們的新的特權方法。 摻元類有一種重用代碼的方法不需要用到嚴格的繼承。 他的做法是:先建立一個包含各種通用方法的類,然後再用他擴充其他類。 這種包含通用方法的類就是摻元類。 下面定義一個簡單的摻元類: 複製代碼var Mix = function(){};Mix.prototype = {    serialize: function(){        var output = [];        for(key in this){            output.push(key+':'+this[key]);        }        return output.join(',');    }}augment(Author, Mix);var author = new Author('Ross', ['js desgin']);複製代碼  augment其實很簡單。他用一個for in將giving class的prototype中的每個成員都添加到receiving class中。 複製代碼function augment(rclass, gclass){    if(arguments[2]){ //giving more than 3 paras        for(var i=2, len=arguments.length; i<len; i++){            rclass.prototype[arguments[i]] = gclass.prototype[arguments[i]];        }    }else{        for(methodName in gclass.prototype){            if(!rclass.prototype[methodName]){                rclass.prototype[methodName] = gclass.prototype[methodName];            }        }        }}複製代碼上面的方法還可以指明只繼承其中的部分成員。  摻元類的重大有點就是可以實現多繼承效果,其他的方法就沒法了,因為一個對象只能有一個原型對象。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.