[JS Method PK] instanceof () vs isprototypeof () hasOwnProperty () vs propertyisenumerable ()
These methods in the high-level programming JS often used, for beginners may not know what the difference between them, I summed up my experience, for your reference:
First, define an object:
1functionParent () {this.name = "Wenbo";}2 Parent.prototype.alertP =function() {3 Alert ("Parent");4}56functionChild () {this.age = 23;}function () {}10 11 function F () {}12 f.prototype = Parent.prototype; 13 child.prototype = new F (); 14 Child.prototype.constructor = Child;15 var child = new Child ();
1,instanceof () vs isprototypeof ():
Instanceof: Determines whether the object is an instance of another object.
1 instanceof //true 2 //
isPrototypeOf: Determines whether an object image is a prototype of an instance.
1 Parent.prototype.isPrototypeOf (child); //true2 Child.prototype.isPrototypeOf (child); true
2, hasOwnProperty () vs propertyisenumerable ():
hasOwnProperty: Determines whether an object has a specific property, (note that the property of an object, not the object's prototype) must be specified with a string.
1 parent.hasownproperty ("name"); //true2 child.hasownproperty ("Age"); true3 Parent.hasownproperty ("ALERTP"); false4 Child.hasownproperty ("ALERTC"); false
propertyIsEnumerable: Determines whether a given property can be enumerated with a for...in statement. Because the for ... in enumeration is a property on the prototype chain, but propertyIsEnumerable is always returned false when it acts on the prototype method, you can assume that for...in can enumerate the properties of the object itself and the properties on the prototype, propertyIsEnumerable can only determine if the property itself can be enumerated. Additionally, predefined properties are not enumerable, and user-defined properties are always enumerated. So if you just want to traverse the properties of the object itself, you can:
For (in obj) {if (Obj.hasownproperty (key) {//dosomething }5}
[JS Method PK] instanceof () vs isprototypeof () hasOwnProperty () vs propertyisenumerable ()