hasOwnProperty () and isprototypeof () attribute examples in JS _javascript techniques

Source: Internet
Author: User
Tags hasownproperty

Both of these properties are provided by Object.prototype: Object.prototype.hasOwnProperty () and Object.prototype.isPropertyOf ()

First explain the hasOwnProperty () method and use. Explain the ispropertyof () method and use

At least understand the prototype chain.

One, Object.prototype.hasOwnProperty ()

Overview

The hasOwnProperty () method is used to determine whether an object contains a specified own property

Grammar

Obj.hasownproperty ("property name");//instance obj contains properties in parentheses, returns True, or False

Describe

All objects inherited from the Object.prototype are inherited from the prototype chain to the hasOwnProperty method, which detects whether an object contains a specific attribute, and in different ways, this method ignores those inherited from the prototype chain.

Instance

1. Use the hasOwnProperty () method to determine whether an object contains specific properties of itself

The following example detects whether object o contains its own properties prop:

var o =new Object ();
O.prop= "exists";
function Change () {
o.newprop=o.prop;
Delete o.prop;
}
O.hasownproperty ("prop")//true change
()//delete O's prop attribute
O.hasownproperty ("prop")//false
// After deletion, the hasOwnProperty () is used to determine if it exists, and the return does not exist.

2. Differences between self and inherited properties

The following example demonstrates the difference between a hasownproperty () method and an inherited property.

var o =new Object ();
O.prop= "exists";
O.hasownproperty ("prop");//true its own property
O.hasownproperty ("toString");//false inherits methods
from the object prototype O.hasownproperty ("hasOwnProperty");//false inherits methods from object prototypes

3. To modify the prototype chain after the hasOwnProperty () point Example

The following example illustrates the difference between the hasOwnProperty () method and the inherited property after modifying the prototype chain

var o={name: ' Jim '};
function person () {
this.age=19;
}
person.prototype=o;//modifies the person's prototype to point to
p.hasownproperty ("name");//false cannot determine the inherited Name property
P.hasownproperty (" Age ");//true;

4. Use hasOwnProperty () to traverse the properties of an object itself

The following example demonstrates how to iterate over an object to ignore inherited properties and get its own properties.

Attention Forin will traverse out enumerable properties in object inheritance

var o={
Gender: ' Male '
}
function Person () {
this.name= "John";
this.age=19;
}
person.prototype=o;
var p =new person ();
For (var k in P) {
if (P.hasownproperty (k)) {
Console.log ("own property:" +k);//name, age
}else{
Console.log ("Inherited properties:" +k);//Gender
}
}

5.hasOwnProperty method is likely to be overwritten

If an object has its own hasOwnProperty () method, the method of hasOwnProperty () on the prototype chain is overwritten

var o={
Gender: ' Male ',
hasownproperty:function () {return
false;
}
}
O.hasownproperty ("gender");/do not write anything will return false
//workaround, using the call method
({}). hasownproperty.call (O, ' gender '); True
Object.prototype.hasOwnProperty.call (o, ' gender ');//true

Second, Object.prototype.isPrototypeOf ()

Overview

The isPrototypeOf () method tests whether an object exists on a prototype chain of another object

Grammar

Object1 is not a prototype of OBJECT2, that is, Object2 is the prototype of Object1, it returns true, otherwise false
object1.isprototypeof (OBJECT2);

Describe

The isPrototypeOf () method allows you to examine a prototype chain that is similar to whether another object exists

Instance

1. Use isPrototypeOf () to check if an object exists on the prototype of another object

var o={};
function person () {};
var p1 =new person ()//inherits from the original prototype, but now has no access to the
person.prototype=o;
var p2 =new person ()//Inherits from O
console.log (o.isprototypeof (p1));//false O is P1 's prototype
Console.log ( O.isprototypeof (p2));//true O is not the prototype of P2

2. Use ispropertyof () to check whether an object exists on a prototype chain of another object

var o={};
function person () {};
var p1 =new person ()//inherits from the original prototype, but now has no access to the
person.prototype=o;
var p2 =new person ()//Inherits from O
console.log (o.isprototypeof (p1));//false O is P1 's prototype
Console.log (o.isprototypeof (p2)); /true O is not a prototype of P2
Console.log (Object.prototype.isPrototypeOf (p1));//true
Console.log ( Object.prototype.isPrototypeOf (p2));//true

The prototype chain structure of P1 is p1=> original person.prototype=>object.prototype=>null

The prototype chain structure of P2 is p2=> o =>object.prototype=>null

P1 and P2 both have object.prototype, so they're all on the Object.prototype prototype chain.

Third, summary

1.hasOwnProperty: It is used to determine whether an object has attributes or objects that you give a name to. However, it is important to note that this method cannot check whether the object's prototype chain has the attribute, which must be a member of the object itself.

2.isPrototypeOf is used to determine whether the object whose prototype chain you want to check exists in the specified object instance, returns True, or returns false.

The above is a small set of JS to introduce the hasOwnProperty () and isprototypeof () attribute examples, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.