您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了“html中創建對象的方式有哪些”,內容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領大家一起研究并學習一下“html中創建對象的方式有哪些”這篇文章吧。
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>創建對象的幾種方式</title> <style> </style> </head> <body> <script> //簡單創建對象 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(); //構造函數模式 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(); //組合使用構造模式和原型模式 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 //動態原型模式 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(); //寄生構造函數模式 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(); //穩妥模式 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中創建對象的方式有哪些”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。