One, instanceof:
instanceof detects if there is a prototype prototype on the right side of the __PROTO__ prototype chain on the left. I've been in two previous articles
[JS Master's Road] the basic characteristics and advantages and disadvantages of the constructor function
[JS Master's Road] step-by-step graphical JavaScript prototype (prototype) object, prototype chain
have been shared.
Function createobj (UName) { this.username = uname; this.showusername = function () { return '; ' } } CreateObj.prototype.showUserName = function () { return this.userName; } var obj1 = new createobj (' Ghostwu ') ; var obj2 = new createobj (' Wei Zhuang '); &nBsp; console.log ( obj1 instanceof CreateObj ); //true console.log ( obj2 instanceof CreateObj ); //true console.log ( obj1 instanceof Object ); // True console.log ( obj2 instanceof Object ); //true
Second, isprototypeof:
If the implicit prototype __proto__ points to an object prototype (Createobj) that calls the isPrototypeOf () method, then this method returns true, such as:
1 var obj1 = new Createobj (' Ghostwu '), 2 var obj2 = new Createobj (' Wei Zhuang '); 3 Console.log (CREATEOBJ.PR Ototype.isprototypeof (obj1)); True4 Console.log (CreateObj.prototype.isPrototypeOf (OBJ2)); True
Because obj1,obj2 of the implicit prototype __proto__ point is Createobj.prototype, a friend may ask Createobj.prototype above there is no isprototypeof this method, how can
Call it?
Yes, yes, but Createobj.prototype's implicit prototype __proto__ points to Object.prototype, and isprototypeof exists on object.prototype, so it can call
Third, object.getprototypeof
Gets the pointer to the implicit prototype (__proto__) of the instance, because Obj1,obj2 's __proto__ all point to Createobj.prototype
var obj1 = new Createobj (' Ghostwu '); var obj2 = new Createobj (' Wei Zhuang '); Console.log (object.getprototypeof (obj1) = = = Createobj.prototype); True Console.log (object.getprototypeof (obj2) = = = Createobj.prototype); True
Four, when the instance accesses properties and methods, follows the nearest lookup principle
Instance first find in their own body, there, stop looking, if not, along the instance of the __proto__ continue to look up, there, stop looking, if not continue along the prototype chain to look up, if
None of the prototype objects, that is undefined.
Function createobj (UName) { this.username = uname; } CreateObj.prototype.showUserName = function () { return this.userName; } CreateObj.prototype.age = 22; var obj1 = new createobj (' Ghostwu '); obj1.age = 20; var obj2 = new createobj (' Wei Zhuang '); Console.log ( obj1.age ),  //20---> from instance Console.log ( obj2.age ); //22---> From prototype objects delete obj1.age; console.log ( obj1.age ),  //22---> from prototype
Five, hasOwnProperty
Determines whether the property is on an instance or on a prototype object, and returns true on the prototype if it is on the instance, false on the original
Function createobj (UName) { this.username = uname; } CreateObj.prototype.showUserName = function () { return this.userName; } CreateObj.prototype.age = 22; var obj1 = new createobj (' Ghostwu '); obj1.age = 20; var obj2 = new createobj (' Wei Zhuang '); Console.log ( obj1.age ),  //20---> from instance Console.log ( obj1.hasownpropeRty ( ' age ' ) ); //true console.log ( obj2.age )  //22---> From prototype object console.log ( Obj2.hasownproperty ( ' age ' ) ); //false Delete obj1.age; console.log ( obj1.age ); / /22---> From prototype console.log ( obj1.hasownproperty ( ' age ) //false );
VI, in operator
Determines whether the property is on an instance or prototype object, and the return value is true whenever a condition is met
Function createobj (UName) { this.username = uname; } CreateObj.prototype.showUserName = function () { return this.userName; } CreateObj.prototype.age = 22; var obj1 = new createobj (' Ghostwu '); obj1.age = 20; console.log ( ' age ' in obj1 ); //true var obj2 = new createobj (' Wei Zhuang '); Console.log ( ' age ' in obj2 ); //true delete obj1.age; console.log ( ' age ' in obj1 ); //true console.log ( ' user ' in obj1 ); //false console.log ( ' user ' in obj2 ); // False
Seven, with the use of in and hasownproperty, you can encapsulate a function to determine whether this property is on a prototype object, and the return value is true: on the Prototype object, false: Not on the prototype object
Function createobj (UName) { this.username = uname; } CreateObj.prototype.showUserName = function () { return this.userName; } CreateObj.prototype.age = 20; function hasprototypeproperty ( obj, name ) { return !obj.hasownproperty ( name ) && ( name in obj ); } var obj1 = new Createobj (' Ghostwu '); var obj2 = new createobj (' Wei Zhuang '); obj1.age = 10; console.log ( hasprototypeproperty ( obj1, ' age ' ) ); //false console.log ( hasprototypeproperty ( obj2, ' age ' ) ); //true
enumerated
Function createobj (UName) { this.username = uname; } CreateObj.prototype.showUserName = function () { return this.userName; } CreateObj.prototype.age = 20; var obj = new createobj ( ' GHOSTWU ' ) for ( var key in obj ) { console.log ( key ); // username,age,showusername } console.log ( object.prototype ); for ( var key in object.prototype ) { console.log ( key );//enumeration, the properties and methods on object.prototype are not enumerated by default, the enumeration property is false }
This article is from the "GHOSTWU" blog, make sure to keep this source http://ghostwu.blog.51cto.com/11192807/1960235
[JS Master's Road] prototype object (prototype) and prototype chain related properties and methods