js物件導向之建立對象

來源:互聯網
上載者:User

標籤:create   組合   屬性   var   設定   建立   執行環境   prototype   lan   

原廠模式
function createPerson(name,age,job){    var o  = new Object();    o.name = name;    o.age = age;    o.job = job;    o.sayName = function(){        alert(this.name);    }    return o;}var person1 = createPerson(‘zy1‘,28,‘new worker‘);var person2 = createPerson(‘zy2‘,29,‘new worker2‘);

原廠模式雖然解決了建立多個相似對象的問題,但卻沒有解決對象識別問題(即怎麼樣知道一個對象的類型)。因為全部都是Object,不像Date、Array等,因此出現了建構函式模式。

 建構函式模式
function Person(name,age,job){    this.name = name;    this.age = age;    this.job = job;    this.sayName = function(){        alert(this.name);    }}var person1 = new Person(‘zy1‘,28,‘new worker‘);var person2 = new Person(‘zy2‘,29,‘new worker2‘);

建構函式模式與原廠模式比較有以下不同

  • 沒有顯式地建立對象
  • 直接將屬性和方法賦給this對象
  • 沒有return語句

這種方法建立的對象都有一個constructor屬性,此例中指向Person,即 person1.constructor == Person


建構函式與普通函數的區別就在於調用的方式,即new出來的。如果不通過new操作,那它跟普通函數沒有什麼兩樣

建構函式的缺點:

建構函式中每個方法都要在每個執行個體上重新建立一遍,如果上面的person1和person2方法中sayName(),不是同一個Function執行個體。因為函數是對象,即每定義一個函數,也就是執行個體化一個對象。

即上面的等價 this.sayName = new Function("alert(this.name)")


上面的的方法也可以改成

function Person(name,age,job){    this.name = name;    this.age = age;    this.job = job;    this.sayName = sayName}function sayName(){    alert(this.name)}

但是這樣sayName()就成為全域函數,很不好,所以引出下面的模式

原型模式

關於原型的具體見http://www.cnblogs.com/myzy/p/6083141.html這裡就不解釋了

使用原型好處是可以讓所有對象執行個體共用它所包含的屬性和方法。

function Person(){}  Person.prototype.name = ‘zy‘;  Person.prototype.age=‘29‘;  Person.sayName = function(){      alert(this.name);  }var person1 = new Person();var person2 = new Person();alert(person1.sayName == person2.sayName) //true

更簡單的原型文法

function Person(){}Person.prototype = {    name:‘zy‘,    contructor:Person,    age:29,    sayName:function(){        alert(this.name);    }}

注意,這裡重新設定了contructor.因這如果不加contructor,這樣方式建立的新對象,建構函式不再指向Person

缺點:如果一個執行個體修改了原型中的屬性值,那麼其他執行個體中這個屬性值也會改變。

組合使用建構函式模式和原型模式
function Person(name,age,job){    this.name = name;    this.age = age;    this.friends=[‘a‘,‘b‘];}Person.prototype = {    contructor:Person,    sayName:function(){        alert(this.name);    }}

這種模式是使用最廣泛的方法。

動態原型模式
function Person(name,age,job){    this.name = name;    this.age = age;    this.friends=[‘a‘,‘b‘];    if(typeof this.sayName != ‘function‘){        alert(this.name);    }}

其中if語句可以是初始化之後應該存在的任何屬性或方法,不必用一大堆if語句檢查每個語句和每個方法,只要檢查其中一個即可。

寄生建構函式模式
function Person(name,age,job){    var o  = new Object();    o.name = name;    o.age = age;    o.job = job;    o.sayName = function(){        alert(this.name);    }    return o;}var person1 = new Person(‘zy1‘,28,‘new worker‘);

這裡了new操作符並把使用的封裝函數叫做建構函式之外,這個模式和原廠模式其實是一模一樣的。

穩妥建構函式模式
function Person(name,age,job){    var o  = new Object();    o.sayName = function(){        alert(name);    }    return o;}var person1 = Person(‘zy1‘,28,‘new worker‘);

這種方法不引用this,new。這種模式提供的這種安全性,使得它非常適合在某些安全執行環境。

 

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.