Use keywords in
The method based on its own attributes and the object to inherit properties exists.
var o={x:1};
"x" in o; //true,自有属性存在
"y" in o; //false
"toString" in o; //true,是一个继承属性
Use object hasOwnProperty () method
var o={x:1};
o.hasOwnProperty("x"); //true,自有属性中有x
o.hasOwnProperty("y"); //false,自有属性中不存在y
o.hasOwnProperty("toString"); //false,这是一个继承属性,但不是自有属性
This method can only judge of its own property exists and to inherit property returns false.
Use undefined judge
Own property and inherit property can judge.
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, then the method can not return the desired results, as follows.
var o={x:undefined};
o.x!==undefined; //false,属性存在,但值是undefined
o.y!==undefined; //false
o.toString!==undefined //true
Analyzing in conditional statements
var o={};
if(o.x) o.x+=1; //如果x是undefine,null,false," ",0或NaN,它将保持不变