Above, we learned about constructors through basic object creation problems, and this article goes on to understand the basic features of constructors and their pros and cons.
Each object has a constructor attribute above it (strictly speaking, it is a prototype, and the object is found by locating the constructor property). I'll explain it in the way I used to say it.
1 functionCreateobj (uName) {2 This. UserName =UName;3 This. Showusername =function () {4 return This. UserName;5 }6 }7 varObj1 =NewCreateobj (' Ghostwu ');8 varObj2 =NewCreateobj (' Wei Zhuang '));9Console.log (Obj1.constructor = = = Createobj);//trueTenConsole.log (Obj2.constructor = = = Createobj);//true
By default, the constructor of an object equals the constructor of the instantiated object, and constructor initially serves to identify the object, but is not particularly accurate because constructor can be modified,
The instanceof keyword is commonly used to identify objects.
What is instanceof?
To understand this keyword, you need to figure out the prototype chain, here, I'll put him out in advance.
Suppose the instanceof operator is L on the left and the RL instanceof R//instanceof operation on the right, by judging if there is r.prototypel.__proto__.__proto__ on the prototype chain of l ... = = = R.prototype? False if present returns true otherwise
Note: The instanceof operation will recursively find the prototype chain of L, i.e. l.__proto__.__proto__.__proto__.__proto__ ... Until the top level is found or found.
So one sentence to understand instanceof's arithmetic rules are:
instanceof detects if there is a prototype prototype on the right side of the __PROTO__ prototype chain on the left.
instanceof // true instanceof // true instanceof // true instanceof // true
Obj1,obj2 is an instance of object because all objects inherit from Object
Borrowing constructors
An empty object that can be borrowed from an existing constructor to complete the copying of properties and methods
1 functionCreateobj (uName) {2 This. UserName =UName;3 This. Showusername =function () {4 return This. UserName;5 }6 }7 varobj =NewObject ();8Createobj.call (obj, ' GHOSTWU ' );9Console.log (Obj.username);//GHOSTWUTenConsole.log (Obj.showusername ());//GHOSTWU
Advantages and disadvantages of constructors
The advantage is the ability to identify objects through instanceof, the disadvantage is that each time an object is instantiated, the properties and methods will be copied over
1 var New Createobj (' Ghostwu '); 2 var New Createobj (' Wei Zhuang '); 3 4 // false
From the above results, it can be seen that obj1.showusername and obj.showusername are not the same "in JS, reference type comparison is address, function is a reference type", but there are two different
memory address, because each object's properties are not the same, this is no problem, but the method executes the same code, so there is no need to copy, there are multiple copies, wasting memory. That's the downside.
How do I resolve a constructor's method to copy multiple questions?
1 functionCreateobj (uName) {2 This. UserName =UName;3 This. Showusername =Showusername;4 }5 functionShowusername () {6 return This. UserName;7 }8 varObj1 =NewCreateobj (' Ghostwu ');9 varObj2 =NewCreateobj (' Wei Zhuang '));TenConsole.log (Obj1.showusername = = = Obj2.showusername);//true
The method of the object points to the same global function showusername, although several replication problems have been solved, but the global function is very easy to be overwritten, that is, we often say pollution global variables.
A better solution?
Use the prototype (prototype) object to write the method on the prototype object of the constructor
1 functionCreateobj (uName) {2 This. UserName =UName;3 }4CreateObj.prototype.showUserName =function(){5 return This. UserName;6 }7 varObj1 =NewCreateobj (' Ghostwu ');8 varObj2 =NewCreateobj (' Wei Zhuang '));9Console.log (Obj1.showusername = = = Obj2.showusername);//true
What are prototype objects, as well as prototype chains? and listen to tell
[JS Master's Road] the basic characteristics and advantages and disadvantages of the constructor function