| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwn | [Back] [Original] |
Get to know MDN better
const object1 = {
prop: "exists",
};
console.log(Object.hasOwn(object1, "prop"));
// Expected output: true
console.log(Object.hasOwn(object1, "toString"));
// Expected output: false
console.log(Object.hasOwn(object1, "undeclaredPropertyValue"));
// Expected output: false
Object.hasOwn(obj, prop)
true false
Object.hasOwn() true null undefined false in
Object.prototype.hasOwnProperty() Object.create(null) hasOwnProperty() Object.prototype.hasOwnProperty() Object.hasOwn()
example prop
const example = {};
Object.hasOwn(example, "prop"); // false 'prop'
example.prop = "exists";
Object.hasOwn(example, "prop"); // true 'prop'
example.prop = null;
Object.hasOwn(example, "prop"); // true null
example.prop = undefined;
Object.hasOwn(example, "prop"); // true undefined
const example = {};
example.prop = "exists";
// `hasOwn` true
Object.hasOwn(example, "prop"); // true
Object.hasOwn(example, "toString"); // false
Object.hasOwn(example, "hasOwnProperty"); // false
// `in` true
"prop" in example; // true
"toString" in example; // true
"hasOwnProperty" in example; // true
const example = { foo: true, bar: true };
for (const name of Object.keys(example)) {
//
}
for...in Object.hasOwn()
const example = { foo: true, bar: true };
for (const name in example) {
if (Object.hasOwn(example, name)) {
//
}
}
Array hasOwn()
const fruits = ["Apple", "Banana", "Watermelon", "Orange"];
Object.hasOwn(fruits, 3); // true ('Orange')
Object.hasOwn(fruits, 4); // false
hasOwnProperty hasOwn() hasOwnProperty()
const foo = {
hasOwnProperty() {
return false;
},
bar: "The dragons be out of office",
};
if (Object.hasOwn(foo, "bar")) {
console.log(foo.bar); //true hasOwnProperty() Object
}
Object.create(null) Object.prototype hasOwnProperty()
const foo = Object.create(null);
foo.prop = "exists";
if (Object.hasOwn(foo, "prop")) {
console.log(foo.prop); //true
}
| ECMAScript 2027 LanguageSpecification # sec-object.hasown |
ObjectObject.assign()Object.create()Object.defineProperties()Object.defineProperty()Object.entries()Object.freeze()Object.fromEntries()Object.getOwnPropertyDescriptor()Object.getOwnPropertyDescriptors()Object.getOwnPropertyNames()Object.getOwnPropertySymbols()getPrototypeOf()Object.groupBy()Object.hasOwn()is()Object.isExtensible()Object.isFrozen()Object.isSealed()Object.keys()Object.preventExtensions()Object.seal()Object.setPrototypeOf()Object.values()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.toString()Object.prototype.valueOf()Object/FunctionObject.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.toString()Object.prototype.valueOf()| Web Proxy Viewer | New URL | Original Page |