loading...

JavaScript – 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 property IsEnumerable() 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
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;

// If o has a property x whose value does not convert to false, double it.
// If x is undefined, null, false, "", 0, or NaN, leave it alone.
if (o.x) o.x *= 2;

Comments are closed.

loading...