ãªãã¸ã§ã¯ãã®ããã©ã«ãå¤(valueOfã¨toStringã®é¢ä¿)ã«ã¤ãã¦
ãã®è¨äºã®å 容ã¯ãã¹ã¦ECMAScript5ã«åºã¥ãã¦ããã®ã§IE8ãªã©ã§ã¯éãçµæã«ãªãã¾ã
以前書いた記事ã§{toString : valueOf}ã¨ããè¨è¿°ã®æå³ã«ã¤ãã¦èããã®ã§ãããã³ã¡ã³ãã§ééããææãã¦é ããã®ã§æ¹ãã¦æ¸ãããã¨æãã¾ãã
ä»åã¯ç°¡åã®ããã«ä»¥ä¸ã®ã³ã¼ãã§è¦ã¦ããã¾ãã
var a = { valueOf:function(){ return false; }, toString:valueOf }; document.write(Number(a)); document.write("<br>"); document.write(String(a));
aã¨ããå¤æ°ã«valueOfã¨toStringã®ï¼ã¤ã®ã¡ã½ãããæã¤ãªãã¸ã§ã¯ãããªãã©ã«ã§è¨å®ãããã®aãããããæååãæ°å¤ã¨ãã¦è©ä¾¡ããå ´åã«è¡¨ç¤ºãããå
容ã確èªããã³ã¼ãã§ãã
ãããå®è¡ããã¨
0 false
ã¨ãªãã¾ãã
æ°å¤ã¨ãã¦è©ä¾¡ããå ´åã¯ã¾ãvalueOfãå¼ã³åºããfalseãè¿ã£ã¦ããã®ã§ãfalseãæ°å¤ã«å¤æããçµæ0ã¨è¡¨ç¤ºããã¦ãã¾ãã
æ°å¤ã¨ãã¦è©ä¾¡ããå ´åã«ã¯ç¹ã«çåããªãã®ã§ããæååã¨ãã¦è©ä¾¡ããå ´åã®æµãã¯è¦ãç®ããããªãè¤éã§ãã
ã¾ã
toString:valueOf
ã¨ããè¨è¿°ã§æãã¦ããvalueOfãä½ãªã®ãã§ãããã®valueOfã¯ç´åã«å®ç¾©ãã¦ããã¡ã½ããã®valueOf(a.valueOf)ã§ã¯ããã¾ãããJavascriptã§ãªãã¸ã§ã¯ãããªãã©ã«ã§è¨è¿°ããå ´åãã®{}å ã§ã»ãã®ããããã£ãåç §ãããã¨ã¯ã§ããªãããã§ãã
//ãã®ããã«ã¯æ¸ããªã var a = { methodA : function(){}, methodB : methodA }; //methodAã¯å®ç¾©ããã¦ãã¾ãããã¨ããã¨ã©ã¼ã«ãªã
以前の記事ã§ã¯ãªãã¸ã§ã¯ãã®ãªãã©ã«è¡¨è¨ã®ãªãã§åç §ãããæªå®ç¾©ã®å¤æ°ã¯Object.prototypeã®ååã¡ã½ãããåç §ããããã ãã¨æ¸ãã¦ããã®ã§ããããã¯å³å¯ã«è¨ãã°æ£ããããã¾ããã§ãããæ£ç¢ºã«ã¯æ¬¡ã®æµãã§Object.prototypeã®ã¡ã½ãããåç §ããã¾ãã
valueOfã¯ã³ã¼ãå
ã§varãã¤ãã¦å®£è¨ãããå¤æ°ã§ã¯ãªããããã¾ãã°ãã¼ãã«åå空éãæ¢ããã¾ããJavascriptã«ããã¦ã°ãã¼ãã«å¤æ°ã¯windowãªãã¸ã§ã¯ãã®ååããããã£ãªã®ã§window.valueOfãåç
§ããã¨ãããã¨ã«ãªãã¾ãã
windowãªãã¸ã§ã¯ãã¯Object.prototypeãç¶æ¿ãã¦ããããããããã¿ã¤ããã§ã¼ã³ããã©ã£ã¦Object.prototype.valueOfãåç
§ããã¾ãã以ä¸ããã®valueOfãObject.prototype.valueOfãåç
§ããæµãã§ããããããååã®è¨äºã®1ã¤ç®ã®ééãã§ãã
2ã¤ç®ã®ééãã¯ååã®è¨äºã§ãããä¸ã¤ã®è¬ãã¨ãã¦æ¸ãã¦ããããªãObject.prototype.valueOfãåç §ãã¦ããã¯ãã®toStringãªã®ã«å®è¡ãã¦ã¿ãã¨ç´åã«å®ç¾©ããvalueOf(ä¸ã®ä¾ã§ããã°a.valueOf)ãå¼ã³åºãã®ãã¨ãããã¨ã«ã¤ãã¦ã§ãã
ä¸ã®ä¾ã§ãæååã¨ãã¦è©ä¾¡ããå ´åã«falseã表示ããã¦ãã¾ããfalseã¯ããã¾ã§ãa.valueOfã®çµæã§ããObject.prototype.valueOfã¨ã¯é¢ä¿ãªãã¯ãã§ãã
以前の記事ã§ã¯Object.prototype.valueOfã¯ã²ãã£ã¨ãããå¼ã³åºããããã³ã«ãèªåããªã¼ãã¼ã©ã¤ãããã¦ããªããã©ããã確èªãã¦ããããããªãããã¨æ¸ãã¦ããã®ã§ããããã¯å
¨ãã®ééãã§ããã
ããã§a.valueOfãå¼ã³åºãããã®ã¯ECMAScript(Javascript)ã®ä»æ§ã§æ±ºãããã¦ããtoStringã¨valueOfã®é¢ä¿ã«çç±ãããã¾ãã
ECMAScript5.1ã®ä»æ§ã®ã8.12.8 [ [DefaultValue] ] (hint)ãã¨ããé
ç®ã«æ¬¡ã®ããã«æ¸ããã¦ãã¾ãã
http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf
(ã¯ã¦ãªè¨æ³ã®é½åã§æä½ã使ããªãã®ã§ãã®é¨åãããã§ããã£ã¦è¡¨ãã¦ãã¾ã) ãªãã¸ã§ã¯ããOãã®å é¨ã¡ã½ããã§ãã[[DefaultValue]]ãæååã¨ãããã³ããä¼´ã£ã¦å¼ã°ããå ´åã¯ä»¥ä¸ã®æé ã§å¦çããã: 1. ãªãã¸ã§ã¯ããOãã®å é¨ã¡ã½ãã[[Get]]ã«"toString"ã¨ããæååãä¸ãã¦å®è¡ããçµæããtoStringãã¨ããã 2. ããIsCallable(ãtoStringã)ãtrueã§ããã°ã a. ãOããthisã¨ããtoStringãã®å é¨ã¡ã½ãã[[Call]]ãå¼æ°ãªãã§å®è¡ããçµæããstrãã¨ããã b. ãããstrããããªããã£ããªå¤ã§ããã°ãstrããè¿ãããã 3. ãªãã¸ã§ã¯ããOãã®å é¨ã¡ã½ãã[[Get]]ã«"valueOf"ã¨ããæååãä¸ãã¦å®è¡ããçµæããvalueOfãã¨ããã 4. ããIsCallable(ãvalueOfã)ãtrueã§ããã°ã a. ãOããthisã¨ããvalueOfãã®å é¨ã¡ã½ãã[[Call]]ãå¼æ°ãªãã§å®è¡ããçµæããvalãã¨ããã b. ãããvalããããªããã£ããªå¤ã§ããã°ãvalããè¿ãããã 5. TypeErrorä¾å¤ãçºçãããã
ãã¤ã³ãã¯2-bã®ã¨ããã§ãtoStringã®æ»ãå¤ãããªããã£ããªå¤ã§ãªããã°ãã®å¤ã¯è¿ãããvalueOfã®è©ä¾¡ã«ç§»ãã¨ããã¨ããã§ãã
åé ã®ä¾ã®
toString:valueOf
ã®valueOfã¯Object.prototype.valueOfãæãã¦ããã®ã§ããèªä½ã¯a.valueOfã¨ã¯é¢ä¿ããªãã®ã§ãããObject.prototype.valueOfãè¿ãã®ã¯å¼ã³åºãããªãã¸ã§ã¯ãèªä½ï¼ããªããã£ããªå¤ã§ã¯ãªãã®ã§å¦çã3.ã®é¨åã«ç§»ã£ãçµæa.valueOfãå®è¡ãããã¨ããæµãã§ããã
ãã®å¦çã«ã¤ãã¦ã¯以前の記事ãæ¸ããå¾ãã¤ãã¤ãç解ã§ããéç¶ã¨ããªãã£ãã®ã§ããã³ã¡ã³ãã§æãã¦ããã ãããããã§ãã£ã¨è ã«è½ã¡ã¾ããã
ã¨ããã§ä¸ã®ä»æ§ãããèªãã¨toStringãå¼ã°ããéã«
IsCallable(toString)
ãfalseãªãã°2.aã¨2.bã®å¦çãé£ã°ãã¦3.ã«ç§»ãããã¨ã«ãªãã¾ãã
ã§ã¯IsCallable()ãfalseãè¿ããããªå¤ã¯ã©ããªãã®ãããããªã®ã§ããããã¯ä»æ§æ¸ã«è¡¨ã§ã¾ã¨ãããã¦ãã¾ãã
http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf
ããã«ããã¨å
é¨ã¡ã½ããCallãæã¤ãªãã¸ã§ã¯ã以å¤ã¯ãã¹ã¦IsCallable()ã§falseã¨ãªããã¨ããããã¾ãã大éæã«ããã°é¢æ°ãªãã¸ã§ã¯ãã§ãªããã°ä½ã§ãããã¨ãããã¨ã®ããã§ãã(ãã¶ã)
ã¤ã¾ãåé ã®ä¾ã®
toString:valueOf
ã®é¨åã¯
toString:1
ã§ã
toString:"ã»ã"
ã§ã
toString:{}
ã§ããã¹ã¦æååã¨ãã¦è©ä¾¡ããå ´åã®çµæã¯åã"false"ã«ãªãã¾ãããªãã®æå³ããªãæ°åã¨ããæ¸ãã¦ããã¨ããããããªãã ããªã®ã§ä½¿ããªãã¨ã¯æãã¾ããã