Object.prototype.hasOwnProperty()
hasOwnProperty() 方法是 实例的一个方法,它返回一个布尔值,指示该对象是否具有指定的属性作为其自身属性(而不是继承的属性)。Object
注意: 在支持 的浏览器中,推荐使用它而不是 Object.hasOwn()hasOwnProperty()。
试一试
const object = {};
object.foo = 42;
console.log(object.hasOwnProperty("foo"));
// Expected output: true
console.log(object.hasOwnProperty("toString"));
// Expected output: false
console.log(object.hasOwnProperty("hasOwnProperty"));
// Expected output: false
语法
hasOwnProperty(prop)
参数
返回值
如果对象具有指定的属性作为自身属性,则返回 true;否则返回 false。
描述
hasOwnProperty() 方法返回 true,如果指定的属性是对象的直接属性——即使其值为 null 或 undefined。如果属性是继承的,或者根本没有声明,该方法返回 false。与 运算符不同,此方法不会检查对象原型链中的指定属性。in
该方法可以应用于大多数 JavaScript 对象,因为大多数对象都继承自 ,因此继承了它的方法。例如,Object 是一个 Array,因此您可以使用 ObjecthasOwnProperty() 方法来检查索引是否存在。
const fruits = ["Apple", "Banana", "Watermelon", "Orange"];
fruits.hasOwnProperty(3); // true ('Orange')
fruits.hasOwnProperty(4); // false - not defined
如果对象重写了该方法,或者该对象是没有原型的对象(因为它们不继承自 Object.prototype),则该方法将不可用。下面将给出这些情况的示例。
示例
使用 hasOwnProperty 测试自身属性是否存在
以下代码展示了如何确定 example 对象是否包含名为 prop 的属性。
const example = {};
example.hasOwnProperty("prop"); // false
example.prop = "exists";
example.hasOwnProperty("prop"); // true - 'prop' has been defined
example.prop = null;
example.hasOwnProperty("prop"); // true - own property exists with value of null
example.prop = undefined;
example.hasOwnProperty("prop"); // true - own property exists with value of undefined
直接属性与继承属性
以下示例区分了直接属性和通过原型链继承的属性。
const example = {};
example.prop = "exists";
// `hasOwnProperty` will only return true for direct properties:
example.hasOwnProperty("prop"); // true
example.hasOwnProperty("toString"); // false
example.hasOwnProperty("hasOwnProperty"); // false
// The `in` operator will return true for direct or inherited properties:
"prop" in example; // true
"toString" in example; // true
"hasOwnProperty" in example; // true
遍历对象的属性
以下示例展示了如何遍历对象的枚举属性而不执行继承的属性。
const buz = {
fog: "stack",
};
for (const 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
}
}
请注意, 循环只遍历可枚举项:循环未发出非可枚举属性并不意味着 for...inhasOwnProperty 本身严格限于可枚举项。您可以使用 遍历非可枚举属性。Object.getOwnPropertyNames()
将 hasOwnProperty 用作属性名
JavaScript 不会保护 hasOwnProperty 这个属性名;具有此名称属性的对象可能会返回错误的结果。
const foo = {
hasOwnProperty() {
return false;
},
bar: "Here be dragons",
};
foo.hasOwnProperty("bar"); // re-implementation always returns false
解决此问题的推荐方法是使用 (在支持它的浏览器中)。其他替代方法包括使用外部的 Object.hasOwn()hasOwnProperty。
const foo = { bar: "Here be dragons" };
// Use Object.hasOwn() method - recommended
Object.hasOwn(foo, "bar"); // true
// Use the hasOwnProperty property from the Object prototype
Object.prototype.hasOwnProperty.call(foo, "bar"); // true
// Use another Object's hasOwnProperty
// and call it with 'this' set to foo
({}).hasOwnProperty.call(foo, "bar"); // true
请注意,在前两种情况下没有新创建的对象。
使用 Object.create(null) 创建的对象
没有原型的对象不继承自 Object.prototype,这使得 hasOwnProperty() 不可用。
const foo = Object.create(null);
foo.prop = "exists";
foo.hasOwnProperty("prop"); // Uncaught TypeError: foo.hasOwnProperty is not a function
在这种情况下,解决方案与上一节相同:优先使用 ,否则使用外部对象的 Object.hasOwn()hasOwnProperty()。
规范
| 规范 |
|---|
| ECMAScript® 2026 语言规范 # sec-object.prototype.hasownproperty |
浏览器兼容性
加载中…