1. Use the in keyword.
This method can determine whether the object's own properties and inherited properties exist.
var o={x:1}; "x" in o; //true, own property exists "y" in o; //false "toString" in o; //true, is an inherited property
2. Use the hasOwnProperty() method of the object.
This method can only determine whether the own property exists, and returns false for inherited properties.
var o={x:1}; o.hasOwnProperty("x"); //true, there is x in its own property o.hasOwnProperty("y"); //false, y does not exist in its own property o.hasOwnProperty("toString"); //false, this is an inherited property, but not an own property
3. Use undefined to judge both
own attributes and inherited attributes.
var o={x:1}; o.x!==undefined; //true o.y!==undefined; //false o.toString!==undefined //true
There is a problem with this method. If the value of the property is undefined, the method cannot return the desired result, as follows.
var o={x:undefined}; ox!==undefined; //false, the property exists, but the value is undefined o.y!==undefined; //false o.toString!==undefined //true
4. Direct judgment in the conditional statement
var o={}; if(ox) o.x+=1; //If x is undefined,null,false," ",0 or NaN, it will remain unchanged
[/size]