JavaScript: The Definitive Guide, Sixth Editio javaScript权威指南(第6版) pdf 文字版-文字版, javascript电子书, 和javascript 有关的电子书:

6.4 Testing Properties

6.4 Testing Properties


JavaScript objects can be thought of as sets of properties, and it is often useful to be able to test for membership in the set—to check whether an object has a property with a given name. You can do this with the in operator, with the hasOwnProperty() and propertyIsEnumerable() methods, or simply by querying the property.

The in operator expects a property name (as a string) on its left side and an object on its right. It returns true if the object has an own property or an inherited property by that name:

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

The hasOwnProperty() method of an object tests whether that object has an own property with the given name. It returns false for inherited properties:

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

The propertyIsEnumerable() refines the hasOwnProperty() test. It returns true only if the named property is an own property and its enumerable attribute is true. Certain built-in properties are not enumerable. Properties created by normal JavaScript code are enumerable unless you’ve used one of the ECMAScript 5 methods shown later to make them nonenumerable.

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

Instead of using the in operator it is often sufficient to simply query the property and use !== to make sure it is not undefined:

var o = { x: 1 }

o.x !== undefined; // true: o has a property x

6.4 Testing Properties | 125

o.y !== undefined; // false: o doesn't have a property y o.toString !== undefined; // true: o inherits a toString property

There is one thing the in operator can do that the simple property access technique shown above cannot do. in can distinguish between properties that do not exist and properties that exist but have been set to undefined. Consider this code:

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

Note that the code above uses the !== operator instead of !=. !== and === distinguish between undefined and null. Sometimes, however, you don’t want to make this distinction:

// If o has a property x whose value is not null or undefined, double it. if (o.x != null) o.x *= 2;

欢迎转载,转载请注明来自一手册:http://yishouce.com/book/1/27642.html
友情链接It题库(ittiku.com)| 版权归yishouce.com所有| 友链等可联系 admin#yishouce.com|粤ICP备16001685号-1