js中常見的建立對象的方法

來源:互聯網
上載者:User

標籤:分享   應該   str   alert   const   var   識別   strong   筆記   

前兩天好好的把高程對象那一塊又讀了下,順便寫點筆記。補一句:代碼都測試過了,應該沒有問題的。可以直接拿到控制台跑!

1.原廠模式

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 = person(‘Nicholas‘, 29, ‘Software Engineer‘);var person2 = person(‘tangyuhui‘, 30, ‘bigboss‘);

原廠模式解決了建立多個相似對象的問題,但是沒有解決對象識別的問題(怎樣知道一個對象的類型)。

2.建構函式模式

function Person(name, age, job) {  this.name = name;  this.age = age;  this.job = job;  this.sayName = function() {    alert(this.name);  } ;}var person1 = new Person(‘Nicholas‘, 29, ‘Software Engineer‘);var person2 = new Person(‘tangyuhui‘, 30, ‘bigboss‘);

相對於原廠模式,存在不同的地方是:

(1)沒有顯示地建立對象

(2)直接將屬性和方法賦給了this對象

(3)沒有return語句

注意:使用建構函式建立新執行個體,實際上會經曆以下四個階段:

(1)建立一個新對象

(2)將建構函式的範圍賦值給新對象(因此this指向了這個新對象)

(3)執行建構函式中的代碼(為這個新對象添加屬性)

(4)返回新對象(這裡是沒有顯示地return才會返回新對象)

注意:建構函式的缺點是執行個體上的方法不是相等的,即不同執行個體上的同名函數是不相等的。

   person1.sayName === person2.sayName   //false

3.原型模式

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

我們建立的每個函數都有一個prototype(原型)屬性,這個屬性是一個指標,指向一個對象,而這個對象是儲存可以由特定類型的所有執行個體共用的屬性和方法。

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

執行個體----原型----建構函式之間的關係如:

 

當然這裡有一個更簡便的寫法:

function Person() {};Person.prototype = {  constructor: Person,  name: ‘Nicholas‘,  age: 29,  job: ‘Software Engineer‘,  sayName: function() {    alert(this.name);  }}

這裡會有一個問題,這種方式重設constructor屬性會導致它的[[Enumerable]]特性被設定為true,所以還有另外一種寫法:

function Person() {};Person.prototype = {  name: ‘Nicholas‘,  age: 29,  job: ‘Software Engineer‘,  sayName: function() {    alert(this.name);  }}Object.defineProperty(Person.prototype, ‘constructor‘, {  enumerable: false,  value: Person})

這裡有一點要注意:如果要重寫原型對象,一定要先重寫後執行個體化,不然執行個體和原型之間就斷了聯絡。類似下面的代碼,會出錯的:

 

function Person() {};var friend= new Person();Person.prototype = {  constructor: Person,  name: ‘Nicholas‘,  age: 29,  job: ‘Software Engineer‘,  sayName: function() {    alert(this.name);  }}friend.sayName();//Error

原理圖如下:

 

 

原型對象代碼量是減少了,但是它缺點也是比較突出的,問題就出在參考型別是按引用傳遞的。看代碼

  function Person(){};  Person.prototype = {    constructor: Person,    name:‘Nicholas‘,    age: 29,    job: ‘Software Engineer‘,    friends: [‘zhangsan‘,‘lisi‘],    sayName: function(){      alert(this.name);    }  }  var person1 = new Person();  var person2 = new Person();  person1.friends.push(‘wanger‘);  console.log(person1.friends);    //zhangsan,lisi,wanger  console.log(person2.friends);    //zhangsan,lisi,wanger

4.組合使用建構函式模式和原型模式

單獨使用原型模式還是有一點問題,建立自訂類型的常見方式,就是使用建構函式模式與原型模式。

function Person(name, age, job) {  this.name = name;  this.age = age;  this.job = job;  this.firends = [‘mayun‘, ‘mahuateng‘]}Person.prototype = {  constructor:  Person,  sayName:  function() {    alert(this.name)  }}var person1 = new Person(‘zhangsan‘, 29, ‘廚師‘);var person2 = new Person(‘lisi‘, 30, ‘工程師‘);person1.firends.push(‘wangwei‘);console.log(person1.firends); //mayun, mahuateng, wangweiconsole.log(person2.firends); //mayun, mahuatengconsole.log(person1.sayName === person2.sayName); //true

5.動態原型模式

如果想把所有的資訊封裝在建構函式中,就可以使用該模式。

function Person(name, age, job) {  this.name = name;  this.age = age;  this.job = job;  if (typeof this.sayName !== ‘function‘) {      Person.prototype.sayName = function(){        alert(this.name);      }  }}var friend = new Person(‘zhangsan‘, 29, ‘大佬‘);

 

6.寄生建構函式模式

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 friend = new Person(‘zhangsan‘, 40, ‘計生辦‘);friend.sayName()  //zhangsan

這種模式和工廠函數模式很像,只是使用new執行個體化對象,使用new建構函式執行個體化函數時會4個過程,如果沒有指定傳回值時,預設返回的是this,但也可以顯示的return。這裡就使用了該特性。這種模式不常用,會用到該模式,真的不常用。反正我沒怎麼用過。

 

7.穩妥建構函式模式

  所謂穩妥對象,指的是沒有公用屬性,而且其他方法也不引用this,穩妥對象最合適在一些安全的環境中或者防止資料被篡改的程式中使用。

function Person(name, age, job) {  var o = new Object();  //定義私人變數和函數   //添加方法  o.sayName = function() {    alert(name);  }  return o;}var friend = Person(‘zhangsan‘, 40, ‘計生辦‘);friend.sayName()  //zhangsan 只有該方法才能訪問到name的值

這裡需要注意的是寄生建構函式模式和穩妥建構函式模式建立的執行個體對象與建構函式沒有什麼關係。

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.