js检测对象属性

In:(检测自身及原型属性)

var o={x:1}; 
"x" in o; //true,自有属性存在 
"y" in o; //false 
"toString" in o; //true,是一个继承属性 

undefined(检测自身及原型属性)

var o={x:1}; 
o.x!==undefined; //true 
o.y!==undefined; //false 
o.toString!==undefined //true 

条件语句中直接判断(检测自身及原型属性)

var o={}; 
if(o.x) o.x+=1; //如果x是undefine,null,false," ",0或NaN,它将保持不变

let a = {};
if(a.toString){
    console.log(a)  //{}
}

hansOwnProperty(检测自身属性)

var o={x:1}; 
o.hasOwnProperty("x");    //true,自有属性中有x 
o.hasOwnProperty("y");    //false,自有属性中不存在y 
o.hasOwnProperty("toString"); //false,这是一个继承属性,但不是自有属性 
原文地址:https://www.cnblogs.com/sghy/p/7809650.html