JavaScript objects can be thought of as a collection of attributes, and we often detect the relationships in the members of the collection and determine whether the property is in this object, so we can pass in, hasOwnProperty (), propertyisenumerable ()
and other methods. These methods are described separately below:
One, in detects whether the property belongs to the object.
The on left side is the property name, the right is the object, and returns True if the object's inherited property or its own property includes this property:
var o={x:1};
"X" in O//true x is the property of O
"ToString" in O//True o inherits the ToString property
The hasOwnProperty () method is used to detect whether a given name is a free property of an object. For an inherited property return false example
var o={x:1};
O.hasownproperty ("x")//true:o Have a free attribute X
O.hasownproperty ("toString"); False ToString is an inherited property:
Third, propertyisenumerable () is a hasownproperty () enhanced version, not only to verify the free property of the object, but also to verify that the property, whether Enumerable. As follows
var o={x:1};
O.propertyisenumerable (x)//True, X is the free property of O and can be enumerated
Object.property.propertyIsEnumerable ("tostirng")//false non-enumerable
You
In addition to using the in operator, the more simple way to use "! = = "Determines whether the property is undefined, as shown in the following code:
var o={x:1};
o.x!==undefined//true O has attribute x
o.tostring!==undefined//true O has inheritance property in ToString
However, there is a situation where you can use the in operator only, not the method above
As follows:
var o={x:undefined};
O.x!==undefined//false Property exists but has a value of undefined
"X" in O//true exists attribute X
JavaScript, O detection properties