您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了“html中創(chuàng)建對(duì)象的方式有哪些”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“html中創(chuàng)建對(duì)象的方式有哪些”這篇文章吧。
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>創(chuàng)建對(duì)象的幾種方式</title> <style> </style> </head> <body> <script> //簡單創(chuàng)建對(duì)象 var person = new Object(); person.name = "nicholas"; person.age = 23; person.job = "IT"; person.sayName = function(){ alert(this.name); }; person.sayName(); //字面量 var person = { name : "nicholas", age : 23, job : "IT", sayName : function(){ alert(this.name); } }; person.sayName();// nicholas //工廠模式 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; } person1 = createPerson("nicholas", 20, "IT"); person1.sayName(); person2 = createPerson("blue", 23, "HR"); person2.sayName(); //構(gòu)造函數(shù)模式 function Person(name, age, job){ this.name = name; this.age = age; this.job = job; this.sayName = sayName; } function sayName(){ alert(this.name); } var person1 = new Person("nicholas", 20, "IT"); person1.sayName(); var person2 = new Person("gred", 22, "HR"); person2.sayName(); //原型模式 function Person(){}; Person.prototype.name = "nicholas"; Person.prototype.age = 20; Person.prototype.job = "IT"; Person.prototype.sayName = function(){ alert(this.name); }; var person1 = new Person() person1.sayName(); var person2 = new Person(); person2.sayName(); alert(person1.sayName == person2.sayName); //原型模式- 簡化 function Person(){}; Person.prototype = { name : "nicholas", age : 20, job : "IT", sayName : function(){ alert(this.name); } }; var person1 = new Person(); person1.sayName(); var person2 = new Person(); person2.sayName(); //組合使用構(gòu)造模式和原型模式 function Person(name, age, job){ this.name = name; this.age = age; this.job = job; this.friends = ["xiaoming","daming"]; } Person.prototype = { constructor : Person, sayName : function(){ alert(this.name); } } var person1 = new Person("nicholas", 20, "IT"); person1.sayName();//nicholas person1.friends.push("Van"); alert(person1.friends);//xiaoming,daming,Van var person2 = new Person("blue", 22, "HR"); person2.sayName();//blue alert(person2.friends);//xiaoming,daming //動(dòng)態(tài)原型模式 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 friends = new Person("nicholas", 20, "IT"); friends.sayName(); //寄生構(gòu)造函數(shù)模式 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("nicholas", 20, "IT"); person1.sayName(); //穩(wěn)妥模式 function Person(name, age, job){ var o = new Object(); o.name = name; o.sayName = function(){ alert(name); }; return O; } var person1 = new Person("nicholas", 20, "IT"); person1.sayName(); </script> </body> </html>
以上是“html中創(chuàng)建對(duì)象的方式有哪些”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。