Let's cut through some confusion here. First, let's simplify by assuming hasOwnProperty
already exists; this is true of the vast majority of current browsers in use.
hasOwnProperty
returns true if the attribute name that is passed to it has been added to the object. It is entirely independent of the actual value assigned to it which may be exactly undefined
.
Hence:
var o = {}
o.x = undefined
var a = o.hasOwnProperty('x') // a is true
var b = o.x === undefined // b is also true
However:
var o = {}
var a = o.hasOwnProperty('x') // a is now false
var b = o.x === undefined // b is still true
The problem is what happens when an object in the prototype chain has an attribute with the value of undefined? hasOwnProperty
will be false for it, and so will !== undefined
. Yet, for..in
will still list it in the enumeration.
The bottom line is there is no cross-browser way (since Internet Explorer doesn't expose __prototype__
) to determine that a specific identifier has not been attached to an object or anything in its prototype chain.