JavaScriptã®ã¡ã½ããã³ã¼ã«ã®ä»çµã¿ãæ·±ãç解ãã ï¼åç §åã¨ã¯ï¼ï¼
ã¯ããã«
JavaScriptã§ã¯é¢æ°ããªãã¸ã§ã¯ãã§ãããã®ãã¨ã¯ããç解ããã¦ããã¨æãã¾ããé¢æ°ã¨ã¡ã½ããã¨ã®æ確ãªéãã¯ãªããã©ã¡ããé¢æ°ãªãã¸ã§ã¯ãã§ãããã¨ããã¨ããã¾ã§ã¯ããã®ã§ãããé¢æ°ã³ã¼ã«ã¨ã¡ã½ããã³ã¼ã«ã®éãããããã¯ãã®ä»çµã¿ã¯æ£ç¢ºã«ç解ããã¦ããã§ãããããå æ¥ãè·å ´ã®å¾è¼©ã«åé¡ãåºããã¨ããæ£ç¢ºã«çãããããªãã£ãã®ã§ããã¾ããï¼ã¨æãããããç¥ãã¾ããããé¢æ°ã³ã¼ã«ã®ä»çµã¿ã解説ãã¾ãã
é¢æ°ã¨ã¡ã½ãã
JavaScriptã§ã¯é¢æ°ã¨ã¡ã½ããã«ã¯æ¬è³ªçãªéãã¯ããã¾ããããªãã¸ã§ã¯ãã®ããããã£ã¨ãã¦å®ç¾©ãããé¢æ°ã便å®çã«ã¡ã½ããã¨å¼ãã§ããã ãã§ããparseInt()ãªã©ã®ã°ãã¼ãã«é¢æ°ãã°ãã¼ãã«ãªãã¸ã§ã¯ãã®ããããã£ã§ãããé¢æ°ã®ä¸ã§ãã¼ã«ã«ã«å®ç¾©ããé¢æ°ãæ¦å¿µçã«ã¯Activation Objectã®ããããã£ãªã®ã§åºæ¬çã«ã¯å ¨ã¦ã®é¢æ°ã¯ã¡ã½ããã§ãã
⻠注ï¼ECMA262 5thã§ã¯Activation Objectã¨ããã¢ãã«ã¯æ¶ãã¦ãã¾ãã代ããEnvironment Recordã¨ãããã®ãç»å ´ãã¾ãã
ç°¡åãªä¾ã§ã¡ã½ããã³ã¼ã«ã®ä»çµã¿ãæ¹ãã¦èãã¦ã¿ã¾ãããã
var value = 987; var obj = { value: 123, getValue: function () { return this.value; } }; var v1 = obj.getValue(); // (J1) => 123 var v2 = (obj.getValue)(); // (J2) => 123 var v3 = (fn = obj.getValue)(); // (J3) => 987
v1ã¨v2ã«ã¯123ãä»£å ¥ããã¾ããããã¯åé¡ãªãã¨æãã¾ããä¸æ¹ãv3ã«ã¯987ãä»£å ¥ããã¾ãã
JavaScriptã¯ã¡ã½ããã¨é¢æ°ã®åºå¥ã¯äºå®ä¸ãªããã©ã¡ããé¢æ°ãªãã¸ã§ã¯ããªã®ã«ã(J3)ã®ä¾ã®ããã«ãã®é¢æ°ãªãã¸ã§ã¯ããå¤æ°ã«ä»£å ¥ãã¦ãã¾ãã¨ã©ããã¦åä½ãå¤ãã£ã¦ãã¾ãã®ã§ããããï¼
ããã¯thisã®å¤ãéããããã¨ããã®ãä¸ã¤ã®ç絡çãªçãã§ããcallãapplyé¢æ°ã使ã£ã¦æ示çã«thisã®å¤ã渡ãã¦ããã°æå¾ ããçµæã«ãªãã¾ãã
var v4 = (fn = obj.getValue).call(obj); // (J4) => 123
ã§ãã(J1)ã¨(J2)ãåããªã(J3)ãåãã§ãããã¨æããããªãã¾ãããï¼ï¼£è¨èªçãªçºæ³ã§ã¯ãè¨ç®ã®éä¸çµæãå¤æ°ã«ä»£å ¥ãã¦ãçµæã¯åãã¯ãã§ãã
JavaScriptã§ã¯C++ãJavaã¨ç°ãªããé¢æ°åãä½ç½®ããé¨åã¯é常ã®å¼ãè¨è¿°ãããã¨ãã§ãã¦ãé¢æ°ã³ã¼ã«ã¯ãã®é¨åãè©ä¾¡ããçµæã«å¯¾ãã¦å®è¡ããã¾ããã(J3)ã®ããã«å¤æ°ã«ä»£å ¥ããã¨ã©ããã¦çµæãç°ãªãã®ã§ããããããããç解ããã«ã¯ãåç §åï¼Reference typeï¼ã¨ãããã®ã使ã£ã¦èª¬æããå¿ è¦ãããã¾ãã
åç §åã¨ã¯ï¼
C++ã«ãåç §åãããã¾ãããããã¨é¡ä¼¼ã®ãã®ã§ãããã£!? JavaScriptã«åç §åããããªãã¦ç¥ããªãã£ãã¨é©ãããã¨æãã¾ããããã®èªèã¯æ£ããã§ããè¨èªã®è¡¨èå°ã«ã¯ç»å ´ããªãåã§ããããããã®ãã®ãåãåºããã¨ã¯ã§ãã¾ããããªã®ã§ãæ®ã©ã®ããã°ã©ãã¯æèãã¦ããªãããããªããã¨æãã¾ããè¨èªã®ã¡ã¿ãªæå³ã¬ãã«ã®è©±ã§ãã
C++ã®åç §åã®å®ä½ã¯ã¢ãã¬ã¹ã§ããã¢ãã¬ã¹ããããã°ããã®ã¢ãã¬ã¹ã«æ¸ããã¦ããå¤ãèªã¿åã£ããï¼åç §å¤ã; dereferenceï¼ãéã«ãã®ã¢ãã¬ã¹ã«å¤ãæ¸ãè¾¼ãã ãã§ãã¾ãã
ã¢ãã¬ã¹ã¯å¤ãæ ¼ç´ããã¦ããå ´æã§ãããJavaScriptã®ã¢ãã¬ã¹ã«ç¸å½ãããã®ã¯ä½ã§ããããï¼JavaScriptã§ã¯å ¨ã¦ã®å¤ã¯ä½ãã®ãªãã¸ã§ã¯ãã®ããããã£ã¨ãã¦å®ç¾©ããã¾ããã¤ã¾ããå¤ãæ ¼ç´ããã¦ããå ´æã§ããã¢ãã¬ã¹ã«ç¸å½ãããã®ã¯ããªãã¸ã§ã¯ãã¨ããããã£åã®çµã¨èããã®ãèªç¶ã§ãããªãã¸ã§ã¯ãã¨ããããã£åããããã°ããã®ããããã£ã®å¤ãåãåºããã¨ãããã®ããããã£ã®å¤ãå¤æ´ãããã¨ãã§ãã¾ããè¦ããã«ãJavaScriptã§ã®åç §åã«ç¸å½ãããã®ã¯ã
JavaScriptã®åç §åï¼ ï¼baseObj, propNameï¼
baseObjã¯ãªãã¸ã§ã¯ããpropNameã¯æååã¨ãã¦ã®ããããã£å
ã¨ããï¼çµã§è¡¨ç¾ããããã¼ã¿æ§é ã¨ãããã¨ã«ãªãã¾ããåç
§åãããã§ã¯Ref
ã®ããã«è¡¨ç¾ãããã¨ã«ãã¾ããåç
§åã«é¢é£ãããªãã¬ã¼ã¿ã«ã¯åç
§å¤ãã«ç¸å½ããGetValue(V)
ã¨ã代å
¥ã«ç¸å½ããPutValue(V,W)
ãããã¾ãã
ã¡ã½ããã³ã¼ã«ã®çå®
JavaScript㧠foo.bar.baz
ãªã©ã®ããã«ããããã£ã¢ã¯ã»ã¹ããããè¨æ³ï¼ã¾ãã¯é£æ³é
åï¼ã§è¡¨ãã¾ããããã®è©ä¾¡çµæã¯å¤ã§ã¯ãªããå®ã¯åç
§ã«ãªã£ã¦ãã¾ãããããå¤æ°ã«ä»£å
¥ãããããã¨ãã«GetValue()ã«ããåç
§å¤ããè¡ããã¦å¤ã«ãªãã¾ãã
ä¸è¨ã®ãããªä»£å
¥æã®å·¦è¾ºå¼ã«ããã表è¨ã§bazããããã£ã®å¤ãå¤æ´ã§ããã®ã左辺å¼ã®foo.bar.baz
ã®è©ä¾¡çµæãåç
§åã ããã§ãã
foo.bar.baz = 123;
ãããç解ãã¦é ããã¨æãã¾ãããobj.func()
ã¨ããã¡ã½ããã³ã¼ã«ã®obj.func
é¨åã®è©ä¾¡çµæã¯å¤ã§ããé¢æ°ãªãã¸ã§ã¯ãã§ã¯ãªããåç
§åã®ãã¼ã¿æ§é ã«ãªãã¾ãã
obj.func
ã¨ããå½¢å¼ã®è©ä¾¡æé ãæ¸ãã¨ã
- objé¨åãè©ä¾¡ããããããããåç
§åã§ããã°GetValue()ããã
- funcé¨åãè©ä¾¡ãããã®ãToString()ããã
- åç
§åRef
ãè¿ãã
ä¸æ¹ãã¡ã½ããã³ã¼ã«obj.func()
ã®æé ã¯ä»¥ä¸ã®ããã«ãªãã¾ãã
obj.func
ãè©ä¾¡ãã¦åç §åRefã®ãã¼ã¿ãæ±ããã
- 1ã®çµæãGetValue()ãã¦é¢æ°ãªãã¸ã§ã¯ããåãåºãã
- 1ã®çµæã®baseObjé¨åãthisã«æç¸ãã¦2ã®çµæã®é¢æ°ãªãã¸ã§ã¯ãã®æ¬ä½ãå®è¡ããã
- 1ã®çµæãGetValue()ãã¦é¢æ°ãªãã¸ã§ã¯ããåãåºãã
ããã(J4)ã®ããã«obj.func
ã®åç
§åãã¼ã¿ãå¤æ°ã«ä»£å
¥ãã¦ãã¾ãã¨ãGetValueã«ããåç
§å¤ããå®è¡ãããããããã®å¤æ°ã«ã¯é¢æ°ãªãã¸ã§ã¯ãèªèº«ã代å
¥ããã¾ããå¾ã£ã¦ãã¡ã½ããã³ã¼ã«ã§ã¯ãªããé常ã®é¢æ°ã³ã¼ã«func()
ã¨åãå½¢å¼ã«ãªãã¾ãã
ããããé常ã®é¢æ°ã³ã¼ã«ãè©ä¾¡æé ã¯ä¸è¨ã®ã¡ã½ããã³ã¼ã«ã®ãã®ã¨å®ã¯å
¨ãåãã§ããobj.func
ãè©ä¾¡ãããã®ã¯åç
§åã§ãããåã«func
ãè©ä¾¡ãããã®ãåç
§åã§ããéãã¯baseObjé¨åãã°ãã¼ãã«ãªãã¸ã§ã¯ãã¨ãªãã ãã§ãã
ä¸æ¹ãé¢æ°ã®ä¸ã§ãã¼ã«ã«ã«å®ç¾©ããå é¨é¢æ°ã®å ´åããã®è©ä¾¡çµæã®åç §åã®baseObjã®å¤ã¯Activation Objectã¨ããã¡ã¿ãªä»®æ³çãªãªãã¸ã§ã¯ãã«ãªãã¾ããããã¦å®éã«é¢æ°ãå®è¡ããéã«ãthisã«ã¯Activation Objectã§ã¯ãªããã°ãã¼ãã«ãªãã¸ã§ã¯ããæç¸ãããã¨ããæé ã«ãªãã¾ãã
é¢æ°ãªãã¸ã§ã¯ããã¡ã½ããã¨ãã¦å®è¡ããå ´åã¨ãé常ã®é¢æ°ã¨ãã¦å®è¡ããå ´åã«thisã®å¤ãç°ãªãã¨ããç¾è±¡ã¯ä»¥ä¸ã®ãããªä»çµã¿ã«åºã¥ãã¦ãã¾ãã
åç §åãç»å ´ãããã®ä»ã®æ§æ
åç §åã¯ä»£å ¥æã®å·¦è¾ºå¼ã¨ã¡ã½ããã³ã¼ã«ä»¥å¤ã«ãç»å ´ãã¾ããã¨ãããããå ¨ã¦ã®å¤æ°ã®è©ä¾¡çµæã¯åç §åã§ãããã ããåç §ã®ã¾ã¾åå¨ãããã¨ã¯ã§ãããããã«GetValue()ã«ããåç §å¤ããå®è¡ãããæ§é ã«ãªã£ã¦ãã¾ãã
ããããä¸é¨ã®æ§æã§ã¯åç §åãã¡ãã£ã¨é¡ãè¦ããããã®ãããã¾ããåç §ã¯æçµçãªå¤ã«ãªãä¸æ©æåã®ç¶æ ãè©ä¾¡ãï¼ã¤é ããããã®ã«è¿ãã¤ã¡ã¼ã¸ã§ãããJavaScriptã§ã¯deleteã¨typeofã«åç §åã使ã£ããã®ãç»å ´ãã¾ãã
deleteã®å¼æ°ã¯è©ä¾¡çµæãåç
§åã§ããã°ããã®ãªãã¸ã§ã¯ãã®ããããã£ãåé¤ãã¾ããåç
§åã§ãªããã°ä½ããã¾ãããdelete hoge
ãå®è¡ããå ´åãå¤æ°hogeã«ä½ãå¤ï¼ä¾ãã°ï¼ï¼ã代å
¥ããã¦ããã¨ãã¦ããdelete 3
ã¨ããæå³ã«ã¯ãªããªãã¨ãããã¨ã§ããããããhogeã®é¨åã¯ã·ã³ãã«ããæ¸ããªãããã§ã¯ãªãä»»æã®å¼ãæ¸ããã¨ãã§ãã¾ããå¼ãè©ä¾¡ããçµæãåç
§åã§ããã°deleteããã®åç
§åã«å¯¾ãã¦é©ç¨ããã¾ããä¸è¨ã®ã³ã¼ãã¯x>y
ã®ã¨ãobjã®"a"ããããã£ãåé¤ãããããã§ãªãã¨ã"b"ããããã£ãåé¤ããã¾ãã
delete ((x > y) ? obj.a : obj.b);
typeofãåæ§ã§ãããç¹ã«ãtypeofã®å¼æ°ãåç
§åã§ããå¿
è¦æ§ãããã®ã¯ãå¤ãä½ããªãæªå®ç¾©å¤æ°ã«å¯¾ãã¦ãtypeofãå¯è½ã«ãªãããã«ããããï¼ãã®å ´åçµæã¯"undefined"ï¼ã§ããåç
§åã§ãªãå¤ã¨ãã¦è©ä¾¡ããã typeof hogehoge
ã§ReferenceErrorãçºçãã¦ãã¾ãã¾ãã
ReferenceErrorã¨ããä¾å¤ã¯å®ã¯ãã®åç
§åã«é¢ä¿ããä¾å¤ã§ããå¤ãä½ãã»ããããã¦ããªãã·ã³ãã«ï¼å¤æ°ï¼ããã®è©ä¾¡çµæã¯åç
§åã«ãªãã¾ããhogehogeã¨ããã·ã³ãã«ãã³ã¼ãä¸ã«çªç¶æ¸ããã¦ãã¦ãããã®æç¹ã§ã¯ã¨ã©ã¼ã§ã¯ãªãåç
§åRef
ä¸è¬ã«ãRefereceErrorã¯GetValue()ã§åç §å¤ãããéã«ãbaseObjãnullã®ã¨ãçºçããã¨ã©ã¼ã§ãã
ãããã«
é¢æ°ã³ã¼ã«ã¨ã¡ã½ããã³ã¼ã«ã®ç°¡åãªéãã«ã¤ãã¦èª¬æããç©ããã§ããããæ£ç¢ºã«èª¬æãããã¨ããã¨åç §åã®è©±ãã©ããã¦ãé¿ãããã¨ãã§ãããä»åã¯ã¡ãã£ã¨é£ãã話ã«ãªã£ã¦ãã¾ãã¾ããããããããããªè©³ç´°ãªãã¨ãç¥ããªãã¦ãJavaScriptã§ããã°ã©ã ã¯åé¡ãªãæ¸ãã¾ããã¾ããããã§èª¬æãããã¨ã¯ECMAScript 3rdã«åºã¥ããã®ã§ããææ°ã®ECMAScript 5thã§ã¯è¥å¹²å¤æ´ããã¦ãã¾ãããåºæ¬ã¯åãã§ãã
Javaãç»å ´ããã¨ããã¤ã³ã¿ããªãã¨ãããã¨ãC/C++ããã°ã©ãã«ã¨ã£ã¦å¤§ããªã¤ã³ãã¯ãããã£ãããã§ãã確ãã«è¨èªã¬ãã«ã§ã¯ãã¤ã³ã¿ããªãã®ã§ããããããä¸èº«ã®å®è£ ã¯éã§ããªãã¸ã§ã¯ããä»£å ¥ãããå¤æ°ã¯å ¨ã¦ãã¤ã³ã¿ã®ãããªãã®ã§ããRubyãLispãªã©ãåæ§ã§ããããã¦JavaScriptãåãã§ããJavaScriptã§ã¯ãã®ãã¤ã³ã¿ã«ç¸å½ãããã®ãåç §åã¨ãã¦ã¢ãã«åãã¦ããã¨èãããã¨ãã§ãã¾ãã