您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
这篇文章主要为大家展示了“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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。