JS學習-建立對象

來源:互聯網
上載者:User

標籤:

1、標準建立對象模式

1 var person = new Object();2 person.name = "Nicholas";3 person.age = 29;4 person.job = "Software Engineer";5 person.sayName = function(){alert(this.name);};

 

2、字面量模式

1 var person = {2     name: "Nicholas",3     age: 29,4     job: "Software Engineer",5     sayName: function(){alert(this.name);}6 };

 

3、原廠模式

 1 function createPerson(name, age, job){ 2     var o = new Object(); 3     o.name = name; 4     o.age = age; 5     o.job = job; 6     o.sayName = function(){alert(this.name);}; 7     return o; 8 } 9 var person1 = createPerson("Nicholas", 29, "Software Engineer");10 var person2 = createPerson("Greg", 27, "Doctor");

 

4、建構函式模式

1 function Person(name, age, job){2     this.name = name;3     this.age = age;4     this.job = job;5     this.sayName = function(){alert(this.name);};6 }7 var person1 = new Person("Nicholas", 29, "Software Engineer");8 var person2 = new Person("Greg", 27, "Doctor");

 

5、原型模式

 1 function Person(){} 2 Person.prototype.name = "Nicholas"; 3 Person.prototype.age = 29; 4 Person.prototype.job = "Software Engineer"; 5 Person.prototype.sayName = function(){alert(this.name);}; 6 var person1 = new Person(); 7 person1.sayName(); //"Nicholas" 8 var person2 = new Person(); 9 person2.sayName(); //"Nicholas"10 alert(person1.sayName == person2.sayName); //true

 

6、組合建構函式和原型模式

 1 function Person(name, age, job){ 2     this.name = name; 3     this.age = age; 4     this.job = job; 5     this.friends = ["Shelby", "Court"]; 6 } 7 Person.prototype = { 8     constructor : Person, 9     sayName : function(){alert(this.name);}10 }11 var person1 = new Person("Nicholas", 29, "Software Engineer");12 var person2 = new Person("Greg", 27, "Doctor");13 person1.friends.push("Van");14 alert(person1.friends); //"Shelby,Count,Van"15 alert(person2.friends); //"Shelby,Count"16 alert(person1.friends === person2.friends); //false17 alert(person1.sayName === person2.sayName); //true

 

7、動態原型模式(這裡只在 sayName()方法不存在的情況下,才會將它添加到原型中。)

 1 function Person(name, age, job){ 2     this.name = name; 3     this.age = age; 4     this.job = job; 5     if (typeof this.sayName != "function"){ 6         Person.prototype.sayName = function(){alert(this.name);}; 7     } 8 } 9 var friend = new Person("Nicholas", 29, "Software Engineer");10 friend.sayName(); 

 

8、寄生建構函式模式(除了使用 new 操作符並把使用的封裝函數叫做建構函式之外,這個模式跟原廠模式其實是一模一樣的。)

 1 function Person(name, age, job){ 2     var o = new Object(); 3     o.name = name; 4     o.age = age; 5     o.job = job; 6     o.sayName = function(){alert(this.name);}; 7     return o; 8 } 9 var friend = new Person("Nicholas", 29, "Software Engineer");10 friend.sayName(); //"Nicholas"

 

9、穩妥建構函式模式(一是新建立對象的執行個體方法不引用 this;二是不使用 new 操作符調用建構函式)

function Person(name, age, job){    var o = new Object();    o.sayName = function(){alert(name);};    return o;}var friend = Person("Nicholas", 29, "Software Engineer");friend.sayName(); //"Nicholas"

 

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.