ããªãã¯JavaScriptãç¥ããªã
ãã®è¨äºã¯Michael Woloszynowiczæ°ã®ããã°Web 2.0 Development And Business Lessonsã®è¨äº"You Don't Know JavaScript"ã翻訳ãããã®ã§ãã本人の許可を得てå ¬éãã¾ããåæã¯2011å¹´4æ16æ¥ã«ãã¹ãããã¾ãããç§èªèº«ã®JavaScriptã®å¦ç¿ãæµ ãã誤訳ã®ææçã¯ã³ã¡ã³ããä¸ããã¨å©ããã¾ãã
åæã¯ä»¥ä¸ã®URLããã
http://www.w2lessons.com/2011/04/you-dont-know-javascript.html
å»å¹´ããããããããããããç¾è±¡ãç®ã«çã¾ãããã«ãªã£ããããã°ã©ãããå°ããã触ã£ã¦ããªãæè¡ã§ãããªãããããã§å±¥æ´æ¸ãèè²ãããã¿ã¼ã³ã«ä½åº¦ãåºä¼ã£ããå¤ãã®è¨èªã§èµ·ãããã¨ã§ã¯ããããæãåæ¶ãåãã¦ããè¨èªãJavaScriptã ã
ããªãã¯èªåãç¥ããªãã¨ãããã¨ãç¥ããªã
ãã®çç±ã¯ãã ããããã¹ã¦ã®Webéçºè ã1ã¤ãããã¯JavaScriptãæ±ãå¿ è¦ã«åºãããããã ãç解ãããªãã§JavaScriptãæ¶ããæããããµããã¢ããã¼ãã¨ãã¦ããã®å ´ãã®ãã«ãµã³ãã«ã³ã¼ããæ¢ãã¦ãã¦ã³ããããã®ã ããã®æã®'å¦ç¿'ã§åé¡ãªã®ã¯ãéçºè ãå®éã«ã¯ãã®è¨èªãå¦ãã§ãããããã®ä¸ãèªåããããç¥ã£ã¦ããé¯è¦ãã¦ãã¾ããã¨ã ãä½å¹´ãJavaScriptã§ä»äºããã¦å¦ãã§ããéç¨ã§ããã£ãã®ã¯ãæ¬å½ã«ç解ããããã«ãªã£ã¦ã¯ããã¦ãç¥ããªãã¨ãããã¨ããããã¨ãããã¨ã ããã®ãã¨ã¯ãã種ã循ç°ãã話ã§ãããããªãã«æ¬å½ã«å¿ è¦ãªã®ã¯ãèªåãããã£ã¦ããªãã¨ãããã¨ãæãã¦ããã人ã§ãããæ¬å½ã®å¦ç¿ã ããµã³ãã«ã³ã¼ããã¤ãªãããããã ãã®åç´ãªonClickãã³ãã©ãformããªãã¼ã·ã§ã³ãããã£ããã¨ããªãã®ã«ãèªãããã«JavaScriptãå±¥æ´æ¸ã«ãªã¹ãã¢ãããã誰ãããã¨ããããé¢æ¥ããããã¦ãããjQueryãDojoã®ãããªãã¬ã¼ã ã¯ã¼ã¯ã使ãç¥ãã¨ãããã¨ã¯ãããã¨ã ãããããã®èå¾ã«ããJavaScriptãæ£ããç解ãããã¨ãªãã«ã¯ããããã®ãã¼ã«ãããããã¹ã¿ã¼ãããã¨ã¯ãªãã ãããJavaScriptã®å¤ãã®è¦ç´ ã表ç¾ããã®ã«ãç§ãåºæ¬ã¬ãã«ãä¸ç´ã¬ãã«ãä¸ç´ã¬ãã«ã®ç¥èã ã¨æãèãã以ä¸ã«ç¤ºãã
JavaScriptç解ã®åºæ¬ã¬ãã«
- åºæ¬çãªããã°ã©ãã³ã°ã®ãã¼ã«ãä¾ãã°ãã«ã¼ããifã¹ãã¼ãã¡ã³ããtry/catchãªã©ãç¥ãã
- é¢æ°å®ç¾©ã¨é©ç¨ã«æ§ã ãªæ¹æ³ããããã¨ãç¥ããç¡åé¢æ°ãåæ§ã
- åºæ¬çãªã¹ã³ã¼ãå®ç¾©ã®ååãã°ãã¼ãã«ã¹ã³ã¼ã(window) 対ãªãã¸ã§ã¯ãã¹ã³ã¼ã(closure)ãç解ããã
- ã³ã³ããã¹ãã®å½¹å²ã¨thiså¤æ°ã®ä½¿ãæ¹ãç解ããã
- ãªãã¸ã§ã¯ãã®ã¤ã³ã¹ã¿ã³ã¹åã¨å®£è¨ã®æ§ã ãªããæ¹ãç解ãããé¢æ°ããªãã¸ã§ã¯ãã§ããã¨ãããã¨ãåæ§ã
- JavaScriptã®æ¯è¼æ¼ç®åãä¾ãã°ã'<'ã'>'ã'=='ã'==='ã§ä½ãfalseã«ãªããããªãã¸ã§ã¯ãã¨stringãã©ã®ããã«ãã¦æ¯è¼ãããããç解ããããã£ã¹ããåæ§ã
- Arrayããªãã¸ã§ã¯ãã®å±æ§ã¨é¢æ°ãã©ãã¤ã³ãã¯ã·ã³ã°ãã¦ãããã¨ããããæ¬å½ã®é åã¨ã©ãéãããï¼ãªãã¸ã§ã¯ããªãã©ã«å¯¾é åãªãã©ã«ï¼ã
JavaScriptç解ã®ä¸ç´ã¬ãã«
- ã¿ã¤ãã¼ãã©ãåãããã¤ã¾ãããã¤ã©ã®ããã«ãã¦å©ç¨ã§ããããã«ãªãããç解ãããéåæã¡ã½ããå¼ã³åºããåæ§ã
- ã³ã¼ã«ããã¯ã¨é¢æ°é©ç¨ãæ·±ãç¥ããä¾ãã°ã'call'ã¨'apply'ã¡ã½ããã§ã³ã³ããã¹ããæä½ãã¦ãé¢æ°ãå¼æ°ã¨ãã¦æ¸¡ããã¨ãªã©(function argument passing)ã
- JSONè¨æ³ã¨evalé¢æ°ãç解ããã
- ã¯ãã¼ã¸ã£ãã³ã¼ãã®ããã©ã¼ãã³ã¹ã«ã©ã®ããã«å½±é¿ãããã©ã¤ãã¼ãå¤æ°ãä½æããã®ã«ã©ã®ããã«å©ç¨ãããããç解ããã
- æãããã(lovely)ã(function(){})() å¼ã³åºãã«æ £ã親ããã
- AJAXã¨ãªãã¸ã§ã¯ãã®ã·ãªã¢ã©ã¤ãº
JavaScriptã®ç解ã®ä¸ç´ã¬ãã«
ï¼è¨³æ³¨ï¼ãã®æ®µè½ã¯å ·ä½çãªç¶æ³ããã¾ãæãæµ®ãã°ããããªãæªããé¨åãããã®ã§JSä¸ç´è ã¯åæãåç §ãã ãããï¼
- ã¡ã½ããã®argumentså¤æ°ãç解ãããããã©ã®ããã«ä½¿ããã¦ãarguments.lengthã«ãã£ã¦é¢æ°ã®ãªã¼ãã¼ãã¼ããè¡ãããããarguments.calleeã«ãã£ã¦å帰å¼ã³åºããè¡ããããarguments.calleeã®ä½¿ç¨ããECMAScript 5ã®Strictã¢ã¼ãããµãã¼ããã¦ããªã*1ãã¨ããããããããã«ãå±éºãªãã¨ã ã¨ãããã¨ãä»ãå ãã¦ããããjQuery(ãã¼ã¸ã§ã³1.4ã¾ã§)ã¨Dojoã®ä¸¡è ããããå©ç¨ãã¦ãããã¨ãã ã
- èªå·±ã¡ã¢åãã«ãªã¼åãé¢æ°ã®é¨åé©ç¨ã®ãããªé«çãªã¯ãã¼ã¸ã£ã®ä½¿ãæ¹
- é¢æ°ã¨htmlãããã¿ã¤ãã³ã°ãã¤ã¾ããããã¿ã¤ããã§ã¼ã³ã¨åºæ¬çãªJavaScriptãªãã¸ã§ã¯ãã¨é¢æ°(ä¾ï¼Array)ã使ã£ã¦ã³ã¼ããå°ãããããã¨ã
- Objectåãinstanceofã¨typeofã®ä½¿ãæ¹
- æ£è¦è¡¨ç¾ã¨æ£è¦è¡¨ç¾ã³ã³ãã¤ã«
- withã¹ãã¼ãã¡ã³ãã¨ããªãããã使ãã¹ãã§ãªãã
- æãé£ããã®ã¯ããããã®ãã¼ã«ãã¹ã¦ãçµã³ã¤ãã¦ããããã§(clean)ãå ç¢ã§ãéãã¡ã³ããã³ã¹ãããããã¯ãã¹ãã©ã¦ã¶å¯¾å¿ã®ã³ã¼ãã«ä»ä¸ãããã¨ã ã
ä¸ç´ã¬ãã«ã®æå¾ã®ãã¤ã³ãã¯ã¨ãããéè¦ã§ãããæãå°éããã®ãé£ãããJavaScriptã®æ æ
¢ãªæ§è³ªãèããã°ãèªåã®ã¢ããªã±ã¼ã·ã§ã³ãã¡ã³ããã³ã¹ã§ããªãã¹ãã²ããã£ã³ã¼ãã®æªå¾ªç°ã¸ã¨ããããé¥ãã¦ãã¾ãããã£ããJavaScriptã¨ããè¨èªèªèº«ãå¦ã¹ã°ããããä½ç³»åãã大ããªã¢ããªã±ã¼ã·ã§ã³ã®ã³ã³ããã¹ãã«ããã¦äºãã«çµã³ã¥ãããã¨ãã§ããããã«ãªã£ã¦ãçã«ãã¹ã¿ã¼ã§ããã ãããããã«ã¯æ°å¹´éã®è¨ç·´ã¨å¤±æãå¿
è¦ã§ãããæ¬1åã§ã¯ç²¾éãããã¨ãã§ããªããç§èªèº«ã¯ãJavaScriptãæ¥å¸¸çã«æ¯æ¥æ°æéãæ°å¹´é使ç¨ãã¦ãã¦ãèªåã®ã³ã¼ããæ¸ãã®ã«ããè¯ãæ¹æ³ãè¦ã¤ãããã¨ãç¶ãã¦ããã¨ããã ã
ãããã£ãçç±ã§ããªã«ãã®ãã¬ã¼ã ã¯ã¼ã¯ã«ä¸è¶³é£ã³ã«ã¸ã£ã³ããããã¨ã¯å±éºã§ããããjQueryã®ã³ã¼ãã¯ã¡ã³ããã³ã¹ä¸å¯è½ã«ãªãå¾åããããDojoã¯èªèº«ã®ã¯ã©ã¹ã¨ããã±ã¼ã¸ã·ã¹ãã ã«ãã£ã¦ããããå©é·ããã
JavaScriptã¯Node.jsãªã©ã«ãããã¾ãããã¯ã¨ã³ãã¾ã§æµ¸éãã¦ããã®ã§ãä¸ã«æããè¦ä»¶ã¯ãwebç¹æã®ç¥èããåé¢ãããã¨ã«ãããwebã®å´é¢ï¼ã¤ã¾ãDOMã¨IEï¼ã¯JavaScriptã«æªãååãä¸ãããã¹ã¦ã®ããã°ã©ããéãä¸ããããããããããã¦ãããJavaScriptãwebã®ã³ã³ããã¹ãã§ä½¿ããã¨ãããªãã°ããã¹ã¦ã®è¯ãéçºè ãç¥ãã¹ã追å äºé ãåå¨ããã
- DOMã¨ãããå¹æçã«æä½ãããã¨ãã¤ã¾ãã追å ãåé¤ããã¼ãã®å¤æ´ã ãããã¹ããã¼ããåæ§ã
- Document fragmentã®ãããªãã¼ã«ã使ã£ã¦ããã©ã¦ã¶ã®åèµ°æ»(re-flow)ãæå°éã«ãããã¨ãå«ãã
- ã¯ãã¹ãã©ã¦ã¶ã«å¯¾å¿ããæ¹æ³ã§DOMããæ å ±ãæ½åºãããã¨ï¼ä¾ï¼styleãpositionãªã©ï¼ããã®ãããªãã¨ã¯ãjQueryãDojoãªã©ã®ãã¬ã¼ã ã¯ã¼ã¯ã§é常ã«ãã¾ããªããã¦ããããCSS対styleã¿ã°ã«ä»£è¡¨ããããããªæ å ±ãæ½åºãããã¨ã¨ä½ç½®ããµã¤ãºãæå®ãããã¨ã®éããç解ãããã¨ãéè¦ã ã
- ã¯ãã¹ãã©ã¦ã¶å¯¾å¿ã§ã¤ãã³ãããã³ããªã³ã°ããã¤ã³ãã£ã³ã°ãã¢ã³ãã¤ã³ãã£ã³ã°ããããªã³ã°ãã¤ã¾ããæã¿ã®ã³ã¼ã«ããã¯ã³ã³ããã¹ããéæããæ¹æ³ãéãã¦è¨ãããããã¯ãã¬ã¼ã ã¯ã¼ã¯çµç±ã§ããã°ãé常ã«ãã¾ããã³ããªã³ã°ããã¦ããããå人ã§ãIEã¨W3Cæ¨æºã®ãã©ã¦ã¶ã¨ã®éããç解ããã¹ãã ã
- Expando対attributeè¨å®ã¨ãããã®ããã©ã¼ãã³ã¹ãéããå称ã¨ã®ä¸ä¸è´(naming discrepancies)ãããã¨ãããã¨ã
- DOMãã¼ããæ½åºããæ£è¦è¡¨ç¾
- ãã©ã¦ã¶ã®æ©è½ãå¹æçã«æ¤åºãã¦ãããããä¸åã«å¥å¥ªãããã¨(graceful degradation)ã
ä¸è¨ã®ãªã¹ãããããããã¨ã¯ãJavaScriptã«ã¯ãalert(myVal)ãmyBtn.onclick = ...以ä¸ã®ãã®ãããããããã¨ããå°çãåå¨ããã¨ãããã¨ã ãã³ããã§ãã以ä¸ã®ãã¨ãããªãå¤ãåå¨ããããæ¸ç±ãèªã¿è¨ç·´ãããã¨ã§ããæ¬ç©ã®JavaScriptããã°ã©ãã«ãªããã¨ãã¨ã¯ã§ããªããããããã¹ã¦ã®ãããã¯ãã«ãã¼ããå大ãªæ¸ç±ããJavaScript: The Good Parts âãè¯ããã¼ããã«ãããã¹ããã©ã¯ãã£ã¹ãã¨ãSecrets of the JavaScript Ninjaãã ãæãåºãã¦ã»ãããJavaScriptãæãã¢ã¯ã»ã¹ããããè¨èªã§ã誰ãããã©ã¦ã¶ãæã£ã¦ãã¦ãã»ããã¢ããã®æéããããããªãã¨ãããã¨ããã·ã³ãã«ãªHTMLãã¼ã¸ãä½ã£ã¦ãä¸ã«æããæ¦å¿µ(concepts)ã¨æ¯ãå§ããããå±¥æ´æ¸ã®èè²ã«ã¤ãã¦ã ããåå¿è ã¬ãã«ãã«ãã¼ãã¦ãä¸ç´ã¬ãã«ã«é£ã³è¾¼ãã§ããã°ãJavaScriptããªã¹ãã«å ãã¦ãããã¨èªããããã³ããããªãã§ãèªåãæãã æ©è½(function)ãéçºãã¦ãããã¨ã«æ°ã¥ãããªããJavaScriptãç¥ã£ã¦ããã¨ä¸»å¼µã§ãããããã¾ã§ã¯JavaScriptãç¥ã£ã¦ãããªã©ã¨å®£ä¼ããªãã§ã»ããã
ç§ãæ¸ãæããJavaScriptã®å´é¢ãããã°ãã³ã¡ã³ãããç¥ããã¦ã»ãããã¾ããJSãä»ã®è¨èªãç¥ã£ã¦ããã¨ä¸»å¼µãã人ã«åºä¼ã£ãçµé¨ãå ±æãã¦ã»ããã
ä»ãå ãã¦ããã¨ãç§ã¯ããã³ãã¨ã³ãéçºè ã§ã¯ãªããããã¯ã¨ã³ãéçºè ã§ããããã«ã¹ã¿ãã¯éçºè ã¸ã¨é²åãã¦ãããä»æ¥ã§ã¯ãã»ã¨ãã©ãã¹ã¦ã®ããã¯ã¨ã³ãéçºè ã¯JavaScriptãå¦ã¶å¿ è¦ããããããããã®è¨äºã®æå³ããã¨ããã ãè¦ä¸ããã¨ããæå³ã¯ãªããJSã«ãããã®ãã¹ã¦ãç¥ãã¨ä¸»å¼µããããã§ã¯ãªããç§ã®å¸æã¯ãJavaScriptã¯åºå¤§ã§ãã¯ãã«ãªè¨èªã§ãè¦ãç®ä»¥ä¸ã®ãã®ãããã®ã ã¨ãããå¤ãã®äººãæ°ã¥ãã¦ã»ããã¨ãããã¨ã ã
ããããã®ãã¹ããæ°ã«å ¥ã£ããTwitterã§私をフォローãã¦ã»ããã
*1:2011/11/19è¿½è¨ ä»¥ä¸ãåèã«ãã¦ãã ãããTogetter:arguments.calleeがstrictモードで禁止な理由