Description: recently watching Addy Osmani wrote the "JavaScript design mode" This book, so remember reading notes and share with you. The content of this article basically comes from that book, if feel good can go to buy a look.
9.1Constructor(constructor) mode
The object constructor is used to create objects of a specific type-prepare objects for use, and accept parameters that the constructor can use to set the values of member properties and methods when the object is first created.
9.1.1 Creating objects
varnewobject=NewObject (); //var newobject={}; //1. Setting Properties directlynewobject.hello1= "Hello 1";Console.log (NEWOBJECT.HELLO1)//2. The Bracket methodnewobject["Hello2"]= "Hello 2"; Console.log (Newobject.hello2); //3 Setting object properties and modifying properties of existing propertiesObject.defineproperty (NewObject, "Hello3", {value:"Hello 3", writable:true, Enumerable:true, Configurable:true }); Console.log (NEWOBJECT.HELLO3); //3 templates to reduce the amount of code when adding multiple properties vardefineprop=function(obj,key,value) {//Config.value=value, the code in the book uses the error: config is not defined, so the code is modified here. varConfig ={value:value, writable:true, Enumerable:true, Configurable:true }; Object.defineproperty (Obj,key,config); }; varPerson=object.create (NULL); Defineprop (person,"Language", "JS"); Defineprop (person,"Birthday", "1989"); Defineprop (person,"Hasbeard",false); //traverse all properties of print person for(varObjinchPerson ) {Console.log (obj+ ': ' +Person[obj]); } //4 Another way to set object propertiesobject.defineproperties (newobject,{"Somekey": {value:"Hello World", writable:true }, "Anotherkey": {value:"Foo Bar", writable:false } }); //print here with For loop empty for unknown reasonConsole.log (Newobject.somekey); Console.log (Newobject.anotherkey); //5 inheritance, subclasses get properties of parent class varDriver=object.create (person); Defineprop (Driver,"TopSpeed", "100mph"); for(varObjinchdriver) {Console.log (obj+ ': ' +Driver[obj]); }
9.1.2 basic Constructor(constructor)
// Simple constructor Mode function Car (model,year,miles) { this. model=model; this. year= year; this. miles=miles; this. tostring=function() { returnthis. model+ "have Done "+this. miles+" Miles "; } }
Question:1. Make inheritance Difficult
2.toString () Such a function is redefined for each new object created by using the car constructor, which is not ideal, since this function should be shared among all car type Instances ( feel like a static method in Java )
9.1.3 with prototype Constructor(constructor)
javascript has a name js when the constructor creates an object, The new object will have all the properties of the constructor prototype. In this way, you can create multiple car objects, and access the same prototypes. So tostring () Such a method would be able to
// Constructors with prototypes function Car (model,year,miles) { this. model=model; this. year= year; this. miles=miles; Car.prototype.toString=function() { returnthis. model+ " Have done ' +this. miles+ "Miles"; };
JS design mode Constructor (constructor)