What are the ways to create objects in html
This article mainly shows you "what are the ways to create objects in html", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "what are the ways to create objects in html" this article.
Several ways to create objects / / simply create objects var person = new Object (); person.name = "nicholas"; person.age = 23 person.job = "IT"; person.sayName = function () {alert (this.name);}; person.sayName (); / / literal var person = {name: "nicholas", age: 23 name: "IT", sayName: function () {alert (this.name);}; person.sayName () / nicholas// factory mode function createPerson (name, age,job) {var o = new Object;o.name = name;o.age = age;o.job = function () {alert (this.name);}; return o;} person1 = createPerson ("nicholas", 20, "IT"); person1.sayName (); person2 = createPerson ("blue", 23, "HR"); person2.sayName (); / / Constructor mode 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 (); / / prototype mode function Person () {}; Person.prototype.name = "nicholas"; Person.prototype.age = 20tPerson.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); / / prototype mode-simplified function Person () {}; Person.prototype = {name: "nicholas", age: 20 person2.sayName: "IT", sayName: function () {alert (this.name);}; var person1 = new Person (); person1.sayName () Var person2 = new Person (); person2.sayName (); / / combine construction pattern and prototype pattern 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 () / / nicholasperson1.friends.push ("Van"); alert (person1.friends); / / xiaoming,daming,Vanvar person2 = new Person ("blue", 22, "HR"); person2.sayName (); / / bluealert (person2.friends); / / xiaoming,daming// dynamic prototype pattern 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 (); / / parasitic constructor model 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 () / / secure mode 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 (); these are all the contents of the article "what are the ways to create objects in html?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!