The hasOwnProperty()
method returns a boolean indicating whether the object has the specified property as its own property (as opposed to inheriting it).
obj.hasOwnProperty(prop)
A Boolean
indicating whether or not the object has the specified property as own property.
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.
hasOwnProperty
returns true even if the value of the property is null
or undefined
.
o = new Object(); o.propOne = null; o.hasOwnProperty('propOne'); // returns true o.propTwo = undefined; o.hasOwnProperty('propTwo'); // returns true
hasOwnProperty
to test for a property's existenceThe following example determines whether the o
object contains a property named prop
:
o = new Object(); o.hasOwnProperty('prop'); // returns false o.prop = 'exists'; o.hasOwnProperty('prop'); // returns true
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
The following example shows how to iterate over the properties of an object without executing on inherited properties. Note that the for...in
loop is already only iterating enumerable items, so one should not assume based on the lack of non-enumerable properties shown in the loop that hasOwnProperty
itself is confined strictly to enumerable items (as with Object.getOwnPropertyNames()
).
var buz = { fog: 'stack' }; for (var name in buz) { if (buz.hasOwnProperty(name)) { console.log('this is fog (' + name + ') for sure. Value: ' + buz[name]); } else { console.log(name); // toString or something else } }
hasOwnProperty
as a property nameJavaScript 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
to get correct results:
var foo = { hasOwnProperty: function() { return false; }, bar: 'Here be dragons' }; foo.hasOwnProperty('bar'); // always returns false // Use another Object's hasOwnProperty // and call it with 'this' set to foo ({}).hasOwnProperty.call(foo, 'bar'); // true // It's also possible to use the hasOwnProperty property // from the Object prototype for this purpose Object.prototype.hasOwnProperty.call(foo, 'bar'); // true
Note that in the last case there are no newly created objects.
Specification | Status | Comment |
---|---|---|
ECMAScript 3rd Edition (ECMA-262) | Standard | Initial definition. Implemented in JavaScript 1.5. |
ECMAScript 5.1 (ECMA-262) The definition of 'Object.prototype.hasOwnProperty' in that specification. | Standard | |
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Object.prototype.hasOwnProperty' in that specification. | Standard | |
ECMAScript Latest Draft (ECMA-262) The definition of 'Object.prototype.hasOwnProperty' in that specification. | Draft |
Desktop | ||||||
---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
Basic support | Yes | Yes | 1 | Yes | Yes | Yes |
Mobile | |||||||
---|---|---|---|---|---|---|---|
Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | |
Basic support | Yes | Yes | Yes | 4 | Yes | Yes | Yes |
Server | |
---|---|
Node.js | |
Basic support | Yes |
//github FabioVergani / js-Polyfill_Object-hasOwnProperty //#Polyfill /* simple: Object.prototype.hasOwnProperty||(Object.prototype.hasOwnProperty=function(x){var o,e=this,p=String(x);return p in e && (o=e.__proto__||e.constructor.prototype,(p in o ===false)||e[p]!== o[p])}); */ //Advanced: (function(w){ var isFunction=w.isFunction||(w.isFunction=function(x){return typeof(x)==='function'}), has=w.has||(w.has=function(o,p){var e=p in o;return {value:e && (e=o[p]) && true,refer:e,valueOf:function(){return this.value}}}), Polyfill=w.PolyfillMethod||(w.PolyfillMethod=function(o,p,x){var e=has(o,p);if(e && (e=isFunction(e.refer))===false){o[p]=x};return e}), theProto=w.Object.prototype; Polyfill(theProto,'hasOwnProperty',function(x){var o,e=this,p=String(x);return p in e && (o=e.__proto__||e.constructor.prototype,(p in o ===false)||e[p]!== o[p])}); })(window); /* var obj1={a:1,b:false}, obj2=Object.create(obj1); obj2.a=2; obj2.c='yeah'; //Polyfilled & Native: console.dir([ obj1.hasOwnProperty('a'),//true obj1.hasOwnProperty('b'),//true obj1.hasOwnProperty('c'),//false obj2.hasOwnProperty('a'),//true obj2.hasOwnProperty('b'),//false obj2.hasOwnProperty('c')//true ]); Object.prototype.hasOwnProperty.call(Object.create(null),'a')//false */
Object.getOwnPropertyNames()
for...in
in
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty