1. Create an object of way

 
 
01_Object constructor mode


<
Body > <-! Mode a: Object constructor mode * routines: first create an empty Object object, then dynamically add properties / methods * Applicable scene: at the start of uncertainty within the object data * Problem: Too many statements -> < Script type = "text / JavaScript" > / * a person: name: "Tom", Age: 12 is * / // create an empty Object object var P = new new Object () P = {} // At this time, the internal data is uncertain // then dynamically add properties / methods p.name = ' Tom ' p.age = 12 is p.setName = function (name) { this.name = name } //测试 console.log(p.name, p.age) p.setName('Bob') console.log(p.name, p.age) </script> </body>
 
02_ object literal

<
Body > <-! Way: object literal mode * Routine: {} used to create the object, while the specified property / method * Application scenario: at the start of data within the object is determined Problem: Multiple object, duplicate Code -> < Script type = "text / JavaScript" > var P = { name: ' Tom ' , Age: 12 is , the setName: function (name) { the this .name = name } } // test console.log (p.name, p.age) p.setName ( ' JACK ' ) the console.log (p.name, p.age) var P2 = { // If the code is repeated to create multiple objects name: ' Bob ' , Age: 13 is , the setName: function (name) { the this .name = name } } </ Script > </ body >
 
 
03_ factory pattern


<
Body > <-! Three ways: the factory model * routines: dynamically created objects by factory function and return * Applicable scene: the need to create multiple objects * Question: object does not have a specific type, are of type Object -> < Script type = "text / JavaScript" > function createPerson (name, Age) { // return an object function ===> factory function var obj = { name: name, Age: Age, the setName: function (name) { the this .name = name } } return obj } // create two individual var p1= createPerson('Tom', 12) var p2 = createPerson('Bob', 13) // p1/p2是Object类型 function createStudent(name, price) { var obj = { name: name, price: price } return obj } var s = createStudent('张三', 12000) // s也是Object </script> </body>
 
 
04 _ Custom Model constructor


<
Body > ! <- Four ways: Custom Model constructor * Routine: Custom constructor creates new new objects by * Application scenario: the object type determines the need to create multiple Problem: Each object has the same data , waste of memory -> < Script type = "text / JavaScript" > // definition type function the Person (name, Age) { the this .name = name the this .age = Age the this .setName = function (name) { the this .name = name } } var P1 = new new the Person ( 'Tom', 12) p1.setName('Jack') console.log(p1.name, p1.age) console.log(p1 instanceof Person) function Student (name, price) { this.name = name this.price = price } var s = new Student('Bob', 13000) console.log(s instanceof Student) var p2 = new Person('JACK', 23) console.log(p1, p2) </script> </body>
 
 
05_ constructor prototype combination patterns +


<
Body > <-! Mode VI: constructors combination pattern prototype + * Routine: Custom constructor initializes the function attributes, methods to the prototype * Application scenario: create a plurality of objects of type determination - > < Script type = "text / JavaScript" > function the Person (name, Age) { // initialization typically only function in the constructor the this .name = name the this .age = Age } Person.prototype.setName = function (name) { the this .name = name } var P1 = new new the Person ( ' Tom', 23) var p2 = new Person('Jack', 24) console.log(p1, p2) </script> </body>

 

Guess you like

Origin www.cnblogs.com/lucy-xyy/p/11820169.html