Boolean.prototype.toString()
Baseline
Widely available
This feature is well established and works across many devices and browser versions. Itâs been available across browsers since 2015å¹´7æ.
toString() 㯠Boolean å¤ã®ã¡ã½ããã§ãæå®ãããè«çå¤ã表ãæååãè¿ãã¾ãã
試ãã¦ã¿ã¾ããã
const flag1 = new Boolean(true);
console.log(flag1.toString());
// äºæ³ãããçµæ: "true"
const flag2 = new Boolean(1);
console.log(flag2.toString());
// äºæ³ãããçµæ: "true"
æ§æ
toString()
弿°
ãªãã
è¿å¤
æå®ãããè«çå¤ã表ãæååã§ãã
解説
Boolean ãªãã¸ã§ã¯ã㯠Object ã® toString ã¡ã½ããã䏿¸ããã¦ããã Object.prototype.toString() ãç¶æ¿ãã¦ãã¾ãããBoolean å¤ã®å ´åãtoString ã¡ã½ããã¯è«çå¤ã®æåå表ç¾ãè¿ãã¾ãããã®æåå㯠"true" ã¾ã㯠"false" ã®ã©ã¡ããã§ãã
ãã® toString() ã¡ã½ããã¯ããã® this å¤ã Boolean ããªããã£ãã¾ãã¯ã©ããã¼ãªãã¸ã§ã¯ãã§ãããã¨ãè¦æ±ããã¾ãã this å¤ããã以å¤ã®å ´åãè«çå¤ã¸ã®å¤æã試ã¿ããã¨ãªã TypeError ãçºçãã¾ãã
Boolean ã«ã¯ [Symbol.toPrimitive]() ã¡ã½ããããªãããã JavaScript ã¯æååãæå¾
ããã³ã³ããã¹ãã§ Boolean ãªãã¸ã§ã¯ãã使ç¨ãããå ´åï¼ä¾ï¼ãã³ãã¬ã¼ããªãã©ã« ãªã©ï¼ãèªåçã« toString() ã¡ã½ãããå¼ã³åºãã¾ãããã ããè«çå¤ã®ããªããã£ãã¯ãæååã¸ã®å¤æãè¡ãéã« toString() ã¡ã½ãããåç
§ãã¾ããã代ããã«ãå
ã® toString() å®è£
ã¨åãã¢ã«ã´ãªãºã ã使ç¨ãã¦ç´æ¥å¤æããã¾ãã
Boolean.prototype.toString = () => "Overridden";
console.log(`${true}`); // "true"
console.log(`${new Boolean(true)}`); // "Overridden"
ä¾
>toString() ã®ä½¿ç¨
const flag = new Boolean(true);
console.log(flag.toString()); // "true"
console.log(false.toString()); // "false"
仿§æ¸
| Specification |
|---|
| ECMAScript® 2027 Language Specification> # sec-boolean.prototype.tostring> |