Core JavaScript 1.5 Reference:Global Objects:Object:hasOwnProperty
From MDC
Contents |
[edit] Summary
Returns a boolean indicating whether the object has the specified property.
[edit] Syntax
hasOwnProperty(prop)
[edit] Parameters
-
prop - The name of the property to test.
[edit] Description
Every object descended from Object inherits the hasOwnProperty method. This method can be used to determine whether an object has the specified property as a direct property of that object; unlike the in operator, this method does not check down the object's prototype chain.
[edit] Examples
[edit] Example: Using hasOwnProperty to test for a property's existence
The following example determines whether the o object contains a property named prop:
o = new Object();
o.prop = 'exists';
function changeO() {
o.newprop = o.prop;
delete o.prop;
}
o.hasOwnProperty('prop'); //returns true
changeO();
o.hasOwnProperty('prop'); //returns false
[edit] Example: Direct versus inherited properties
The following example differentiates between direct properties and properties inherited through the prototype chain:
o = new Object();
o.prop = 'exists';
o.hasOwnProperty('prop'); // returns true
o.hasOwnProperty('toString'); // returns false
o.hasOwnProperty('hasOwnProperty'); // returns false