首页 > 代码库 > Testing properties
Testing properties
You can do this with the in operator, with the hasOwnProperty() and propertyIsEnumerable() methods, or simply by quering the property.
var o = { x: 1 }"x" in o; // true: o has an own property "x""y" in o; // false: o doesn‘t have a property "y""toString" in o; // true: o inherits a toString property
var o = { x: 1 }o.hasOwnProperty("x"); // true: o has an own property xo.hasOwnProperty("y"); // false: o doesn‘t have a property yo.hasOwnProperty("toString"); // false: toString is an inherited property
var o = inherit({ y: 2 });o.x = 1;o.propertyIsEnumerable("x"); // true: o has an own enumerable property xo.propertyIsEnumerable("y"); // false: y is inherited, not ownObject.prototype.propertyIsEnumerable("toString"); // false: not enumerable
var o = { x: undefined } // Property is explicitly set to undefinedo.x !== undefined // false: property exists but is undefinedo.y !== undefined // false: property doesn‘t even exist"x" in o // true: the property exists"y" in o // false: the property doesn‘t existsdelete o.x; // Delete the property x"x" in o // false: it doesn‘t exist anymore
Testing properties
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。