Baseline Widely available
妿æå®ç对象èªèº«ææå®ç屿§ï¼åéææ¹æ³ Object.hasOwn()
è¿å true
ã妿屿§æ¯ç»§æ¿çæè
ä¸åå¨ï¼è¯¥æ¹æ³è¿å false
ã
夿³¨ï¼ Object.hasOwn()
æ¨å¨å代 Object.prototype.hasOwnProperty()
ã
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
è¯æ³ åæ°
obj
è¦æµè¯ç JavaScript å®ä¾å¯¹è±¡ã
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 çé®é¢æ¡ä¾
æ¬é¨åè¯æäºå½±å 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ââæ è®ºå¯¹è±¡æ¯å¦ä½å建çï¼å®é½å¯ä»¥è¿è¡ã
}
è§è æµè§å¨å
¼å®¹æ§ åè§
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4