hasOwnProperty

Parent Previous Next

Intro

Objects

The prototype

hasOwnProperty

Functions

How this works

Closures and references

The arguments object

Scopes and namespaces

Constructors

Equality and comparisons

Arrays

The Array constructor

The for in loop

The typeof operator

The instanceof operator

Type casting

undefined and null

Reasons against eval

setTimeout and setInterval

Automatic semicolon insertion

hasOwnProperty

In order to check whether a object has a property defined on itself and not somewhere on its prototype chain, it is necessary to use the hasOwnProperty method which all objects inherit from Object.prototype.

Note: It is not enough to check whether a property is undefined. The property might very well exist, but its value just happens to be set to undefined.

hasOwnProperty is the only thing in JavaScript which deals with properties and does not traverse the prototype chain.

// Poisoning Object.prototype
Object.prototype.bar = 1;
var foo = {goo: undefined};
foo.bar; // 1
'bar' in foo; // true
foo.hasOwnProperty('bar'); // false
foo.hasOwnProperty('goo'); // true

Only hasOwnProperty will give the correct and expected result, this is essential when iterating over the properties of any object. There is no other way to exclude properties that are not defined on the object itself, but somewhere on its prototype chain.

hasOwnProperty as a property

JavaScript does not protect the property name hasOwnProperty; thus, if the possibility exists that an object might have a property with this name, it is necessary to use an external hasOwnProperty in order to get correct results.

var foo = {
   hasOwnProperty: function() {
       return false;
   },
   bar: 'Here be dragons'
};
foo.hasOwnProperty('bar'); // always returns false
// Use another hasOwnProperty and call it with 'this' set to foo
{}.hasOwnProperty.call(foo, 'bar'); // true

In conclusion

When checking for the existence of a property on a object, hasOwnProperty is the only method of doing so. It is also recommended to make hasOwnPropertypart of every for in loop, this will avoid errors from extended native prototypes.

Created with the Personal Edition of HelpNDoc: Free help authoring tool