[JS Method PK] instanceof () vs isprototypeof () hasOwnProperty () vs propertyisenumerable ()

Source: Internet
Author: User
Tags hasownproperty

[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 ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.