Javascript检查对象是否存在某个属性


1、hasOwnProperty
var o={x:1};
o.hasOwnProperty("x");       //true,自有属性中有x
o.hasOwnProperty("y");       //false,自有属性中不存在y
o.hasOwnProperty("toString"); //false,这是一个继承属性,但不是自有属性

    使用undefined判断

自有属性和继承属性均可判断。

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

原文地址:https://www.cnblogs.com/pascal1000/p/10849569.html