码迷,mamicode.com
首页 > 其他好文 > 详细

Testing properties

时间:2014-10-10 19:04:54      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   os   ar   strong   sp   div   on   

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 x
o.hasOwnProperty("y");        // false: o doesn‘t have a property y
o.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 x
o.propertyIsEnumerable("y");  // false: y is inherited, not own
Object.prototype.propertyIsEnumerable("toString"); // false: not enumerable
var o = { x: undefined }   // Property is explicitly set to undefined
o.x !== undefined          // false: property exists but is undefined
o.y !== undefined          // false: property doesn‘t even exist
"x" in o                   // true: the property exists
"y" in o                   // false: the property doesn‘t exists
delete o.x;                // Delete the property x
"x" in o                   // false: it doesn‘t exist anymore

 

Testing properties

标签:style   blog   color   os   ar   strong   sp   div   on   

原文地址:http://www.cnblogs.com/woodynd/p/4016472.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!