JS中創造構建函數的方法

來源:互聯網
上載者:User

標籤:模式   turn   code   this   object   函數   class   cti   return   

1.原廠模式

1 function CreatObject(){2     var obj = new Object();3     obj.name = "zackbee";4     obj.sex = "unknow";5     return zackbee;6 }

2.建構函式模式

1 function CreatObject(){2     this.name ="zackbee";3     this.sex = "unknow";4 }5 var obj = new CreatObject;

3.原型模式

1 function CreatObject(){2 }3 CreatObject.prototype.name = "zackbee";4 CreatObject.prototype.sex = "unknow";5 var obj = new CreatObject();

4.原型模式重構原型(有坑)

1 function CreatObject(){2 }3 CreatObject.prototype = {4    name : "zackbee",5    sex : "unknow" 6 };

  1.如果先建立執行個體再使用這樣的方法,執行個體中的prototype與構建函數的prototype不對應(詳情參考高程三p155)

5.組合使用建構函式模式以及原型模式

  略

6.動態原型模式

  如題,略

7.寄生建構函式模式

  代碼與工廠一模一樣,不推薦

8.穩妥模式(適用於安全環境)

  參考高程三p161

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.