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
布尔
indicating whether or not the object has the specified property as own property.
All descendents of
Object
inherit 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 for a property in the object's prototype chain. If an
Object
是
Array
,
hasOwnProperty
method can check whether an index exists.
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
The 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
}
}
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
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.
| 规范 |
|---|
|
ECMAScript (ECMA-262)
The definition of 'Object.prototype.hasOwnProperty' in that specification. |
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
hasOwnProperty
|
Chrome 1 | Edge 12 | Firefox 1 | IE 5.5 | Opera 5 | Safari 3 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 10.1 | Safari iOS 1 | Samsung Internet Android 1.0 | nodejs Yes |
完整支持
Object.getOwnPropertyNames()
for...in
in
Object
Object.assign()
Object.create()
Object.defineProperties()
Object.defineProperty()
Object.entries()
Object.freeze()
Object.fromEntries()
Object.getOwnPropertyDescriptor()
Object.getOwnPropertyDescriptors()
Object.getOwnPropertyNames()
Object.getOwnPropertySymbols()
Object.getPrototypeOf()
Object.is()
Object.isExtensible()
Object.isFrozen()
Object.isSealed()
Object.keys()
Object.preventExtensions()
Object.prototype.__defineGetter__()
Object.prototype.__defineSetter__()
Object.prototype.__lookupGetter__()
Object.prototype.__lookupSetter__()
Object.prototype.hasOwnProperty()
Object.prototype.isPrototypeOf()
Object.prototype.propertyIsEnumerable()
Object.prototype.toLocaleString()
Object.prototype.toSource()
Object.prototype.toString()
Object.prototype.valueOf()
Object.seal()
Object.setPrototypeOf()
Object.values()
Function