Baseline Widely available
hasOwnProperty()
㯠Object
ã¤ã³ã¹ã¿ã³ã¹ã®ã¡ã½ããã§ããªãã¸ã§ã¯ãèªèº«ãï¼ç¶æ¿ããã¦ããªãï¼æå®ãããããããã£ãæã£ã¦ãããã©ããã示ãè«çå¤ãè¿ãã¾ãã
ã¡ã¢: Object.hasOwn()
㯠hasOwnProperty()
ããããã©ã¦ã¶ã¼ã®å¯¾å¿ç¶æ³ã®é¢ã§æ¨å¥¨ããã¾ãã
const object1 = {};
object1.property1 = 42;
console.log(object1.hasOwnProperty("property1"));
// Expected output: true
console.log(object1.hasOwnProperty("toString"));
// Expected output: false
console.log(object1.hasOwnProperty("hasOwnProperty"));
// Expected output: false
æ§æ 弿°
prop
ãã¹ãããããããã£ã®ååã®æååã¾ãã¯ã·ã³ãã«ã
ãªãã¸ã§ã¯ããæå®ããããããã£ãèªåèªèº«ã§ä¿æãã¦ããå ´å㯠true
ãè¿ããããã§ãªãå ´å㯠false
ãè¿ãã¾ãã
hasOwnProperty()
ã¡ã½ããã¯ãæå®ããããããã£ããªãã¸ã§ã¯ãã®ç´æ¥ã®ããããã£ã§ããå ´å (ãã¨ããã®å¤ã null
ã¾ã㯠undefined
ã§ãã£ã¦ã) ã true
ãè¿ãã¾ããããããã£ãç¶æ¿ããã¦ããããã¾ã£ãã宣è¨ããã¦ããªãå ´å㯠false
ãè¿ãã¾ãã in
æ¼ç®åã¨ã¯ç°ãªãããã®ã¡ã½ããã¯ãªãã¸ã§ã¯ãã®ãããã¿ã¤ããã§ã¼ã³ã«æå®ããããããã£ããããã©ããã調ã¹ã¾ããã
ãªããªããã»ã¨ãã©ã®ãªãã¸ã§ã¯ã㯠Object
ã®åå«ã§ããããã®ã¡ã½ãããç¶æ¿ãã¦ããããã§ããä¾ãã°é
å (Array
) ã¯ãªãã¸ã§ã¯ã Object
ãªã®ã§ãã¤ã³ããã¯ã¹ãåå¨ãããã©ããã調ã¹ãã«ã¯ hasOwnProperty()
ã¡ã½ããã使ç¨ãããã¨ãã§ãã¾ãã
const fruits = ["Apple", "Banana", "Watermelon", "Orange"];
fruits.hasOwnProperty(3); // true ('Orange')
fruits.hasOwnProperty(4); // false - not defined
ãã®ã¡ã½ããã¯ãåå®è£
ããããªãã¸ã§ã¯ããã null
ãããã¿ã¤ããªãã¸ã§ã¯ãï¼Object.prototype
ãç¶æ¿ãã¦ããªãï¼ã§ã¯å©ç¨ã§ãã¾ããããããã®å ´åã®ä¾ã¯ä¸è¨ã®éãã§ãã
ãªãã¸ã§ã¯ã o
ã prop
ã¨ããååã®ããããã£ãæã£ã¦ãããã©ãããç¹å®ããä¾ã以ä¸ã«ç¤ºãã¾ãã
const example = {};
example.hasOwnProperty("prop"); // false
example.prop = "exists";
example.hasOwnProperty("prop"); // true - 'prop' ãå®ç¾©ããã¦ãã
example.prop = null;
example.hasOwnProperty("prop"); // true - null å¤ãæã¤ç¬èªããããã£
example.prop = undefined;
example.hasOwnProperty("prop"); // true - undefined å¤ãæã¤ç¬èªããããã£
ç´æ¥ã®ããããã£ã¨ç¶æ¿ãããããããã£
以ä¸ã®ä¾ã§ã¯ãç´æ¥ã®ããããã£ã¨ãããã¿ã¤ããã§ã¼ã³ãéãã¦ç¶æ¿ãããããããã£ãåºå¥ãã¾ãã
const example = {};
example.prop = "exists";
// `hasOwnProperty` ã¯ç´æ¥ã®ããããã£ã«ã¤ãã¦ã®ã¿ true ãè¿ã
example.hasOwnProperty("prop"); // true
example.hasOwnProperty("toString"); // false
example.hasOwnProperty("hasOwnProperty"); // false
// æ¼ç®å `in` ã¯ãç´æ¥ã¾ãã¯ç¶æ¿ãããããããã£ã«å¯¾ã㦠true ãè¿ã
"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...in
ã«ã¼ãã§ãã§ã«åæå¯è½ãªã¢ã¤ãã ã®ã¿ãå復å¦çãããã®ã§ã hasOwnProperty
èªä½ã¯åæå¯è½ãªã¢ã¤ãã ã«å³å¯ã«éå®ããã¦ãããããã«ã¼ãå
ã«åæã§ããªãããããã£ãè¦ãããªããã¨ã«åºã¥ãã¦æ³å®ããã¹ãã§ã¯ããã¾ãã (Object.getOwnPropertyNames()
ã®ããã«)ã
JavaScript 㯠hasOwnProperty
ã¨ããããããã£åãä¿è·ãã¦ãã¾ããããã®ååãæã£ãããããã£ãæã¤ãªãã¸ã§ã¯ãã§ã¯ãæ£ãããªãçµæãè¿ãå¯è½æ§ãããã¾ãã
const foo = {
hasOwnProperty() {
return false;
},
bar: "Here be dragons",
};
foo.hasOwnProperty("bar"); // åå®è£
ã§ã¯å¸¸ã« 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
// å¥ãª Object ã® hasOwnProperty 使ç¨ãã¦ã
// this ã foo ã«è¨å®ãã¦å¼ã³åºã
({}).hasOwnProperty.call(foo, "bar"); // true
ãªããå¾è ã®å ´åã¯æ°ãããªãã¸ã§ã¯ããçæãã¾ããã
Object.create(null) ã§ä½æããããªãã¸ã§ã¯ãnull
ãããã¿ã¤ããªãã¸ã§ã¯ã㯠Object.prototype
ãç¶æ¿ãã¦ããªãã®ã§ã hasOwnProperty()
ã¯ã¢ã¯ã»ã¹ä¸å¯ã«ãªãã¾ãã
const foo = Object.create(null);
foo.prop = "exists";
foo.hasOwnProperty("prop"); // Uncaught TypeError: foo.hasOwnProperty ã¯é¢æ°ã§ã¯ãªã
ãã®å ´åã®è§£æ±ºçã¯åã®ç¯ã¨åãã§ããæ§æã«ã¤ãã¦ã¯ Object.hasOwn()
ã使ç¨ããããã§ãªããã°å¤é¨ãªãã¸ã§ã¯ãã® hasOwnProperty()
ã使ç¨ãã¦ãã ããã
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