+
80
-

js如何检测对象是否存在某个属性?

请问高手,js如何检测对象是否存在某个属性?

网友回复

+
0
-

hasOwnProperty方法可以实现

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

我知道答案,我要回答