Baseline Widely available
Symbol.toPrimitive
ì ì ë°ì´í° ìì±ì ì ìë ¤ì§ ì¬ë³¼ @@toPrimitive
를 ëíë
ëë¤. 모ë íì
ê°ì ë³í ìê³ ë¦¬ì¦ì ê°ì²´ìì ì´ ì¬ë³¼ì ì°¾ìì ì í¸íë ì íì íì©íê³ ê°ì²´ì ìì ííì ë°ííë ë©ìë를 ì°¾ì ë¤ì ê°ì²´ì valueOf()
ë° toString()
ë©ìë를 ë¤ì ì¬ì©í©ëë¤.
const object1 = {
[Symbol.toPrimitive](hint) {
if (hint === "number") {
return 42;
}
return null;
},
};
console.log(+object1);
// Expected output: 42
ê°
ì ìë ¤ì§ ì¬ë³¼ @@toPrimitive
.
í¨ì ê°ì¼ë¡ ì¬ì©ëë Symbol.toPrimitive
ìì±ì ëìì¼ë¡ ê°ì²´ë¥¼ ìì ê°ì¼ë¡ ë³íí ì ììµëë¤. í¨ìë ê²°ê³¼ë¡ ëì¨ ìì ê°ì ì í¸ ì íì ì§ì íë 문ìì´ ì¸ì hint
ì í¨ê» í¸ì¶ë©ëë¤. hint
ì¸ìë "number"
"string"
, "default"
ì¤ íëì¼ ì ììµëë¤.
"number"
íí¸ë ì«ì ê°ì ìê³ ë¦¬ì¦ìì ì¬ì©ë©ëë¤. "string"
íí¸ë 문ìì´ ê°ì ìê³ ë¦¬ì¦ìì ì¬ì©ë©ëë¤. "default"
íí¸ë ììí ê°ì ìê³ ë¦¬ì¦ìì ì¬ì©ë©ëë¤. hint
ë Symbol.prototype[@@toPrimitive]()
ì ê°ì´ ì í¸ ì¬íì ëë¡ë§ ìì©íë©°, 구íì ì´ë¥¼ 무ìí ì ììµëë¤. ì¸ì´ììë hint
ì ê²°ê³¼ ì í ê°ì ì ë ¬ì ê°ì íì§ ìì§ë§, [@@toPrimitive]()
ë ììíì ë°íí´ì¼ íë©°, ê·¸ë ì§ ìì¼ë©´ TypeError
ê° ë°ìí©ëë¤.
@@toPrimitive
ìì±ì´ ìë ê°ì²´ë valueOf()
ë° toString()
ë©ìë를 ë¤ë¥¸ ììë¡ í¸ì¶íì¬ ììíì¼ë¡ ë³íëë©°, ì´ì ëí ìì¸í ì¤ëª
ì íì
ê°ì ì¹ì
ìì íì¸í ì ììµëë¤. @@toPrimitive
를 ì¬ì©íë©´ ììí ë³í ì 차를 ìì í ì ì´í ì ììµëë¤. ì를 ë¤ì´, Date.prototype[@@toPrimitive]
ë "default"
를 "string"
ì¼ë¡ ì·¨ê¸íì¬ valueOf()
ëì toString()
ì í¸ì¶í©ëë¤. Symbol.prototype[@@toPrimitive]
ë íí¸ë¥¼ 무ìíê³ íì ì¬ë³¼ì ë°ííë¯ë¡ 문ìì´ ì»¨í
ì¤í¸ììë Symbol.prototype.toString()
ì´ í¸ì¶ëì§ ìì¼ë©°, Symbol
ê°ì²´ë íì String()
ì íµí´ ëª
ìì ì¼ë¡ 문ìì´ë¡ ë³íí´ì¼ í©ëë¤.
ë¤ì ìì ììë Symbol.toPrimitive
ìì±ì´ ê°ì²´ìì ë³íë ììê°ì ìì íë ë°©ë²ì ì¤ëª
í©ëë¤.
// Symbol.toPrimitive ìì±ì´ ìë ê°ì²´.
const obj1 = {};
console.log(+obj1); // NaN
console.log(`${obj1}`); // "[object Object]"
console.log(obj1 + ""); // "[object Object]"
// Symbol.toPrimitive ìì±ì´ ìë ê°ì²´.
const obj2 = {
[Symbol.toPrimitive](hint) {
if (hint === "number") {
return 10;
}
if (hint === "string") {
return "hello";
}
return true;
},
};
console.log(+obj2); // 10 â hint is "number"
console.log(`${obj2}`); // "hello" â hint is "string"
console.log(obj2 + ""); // "true" â hint is "default"
ëª
ì¸ì ë¸ë¼ì°ì í¸íì± ê°ì´ 보기
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