解決編: JavaScriptã§é åããªãã¸ã§ã¯ãã®ãã¼ãå復ããã¤ãã£ãªã
å æ¥ã®ç¶ãã§ãã
escape_artistãããã詳細ãªã³ã¡ã³ããé ãã¾ããããããã¨ããããã¾ãã
ã¾ãã身å ã«ããã¤ãã£ãã®forã使ã£ã¦ã¯ãããªããã®çæãèãã¦ã¿ã¾ããã
ãã®çµæãçåã解決ããã®ã§ã¡ã¢ãã¦ããã¾ãã
çå1: for inã§é åãã«ã¼ããã¦ã¯ãããªãã®ï¼
使ããªãã»ããããããã§ããçç±ã¯æ¬¡ã®ã¨ããã§ãã
- Arrayã®prototypeãæ¡å¼µããã¦ãããªã¹ã¯ããããã
- é åè¦ç´ 以å¤ã®ããããã£ã追å ããã¦ãããªã¹ã¯ããããã
- ãµã¤ãã®forã®æ¹ãããã©ã¼ãã³ã¹ããããã
çç±1: Arrayã®prototypeãæ¡å¼µããã¦ãããªã¹ã¯ããããã
escape_artistããã«é ããã³ã¡ã³ããåãããããã®ã§ããã®ã¾ã¾å¼ç¨ããã¦ããã ãã¾ãã
inã¯ä»°ãã¨ãããããã¿ã¤ãã辿ããããArray.prototypeã«ä½ãã¡ã½ããã追å ããã¦ããã¨ãããåæããã¦ãã¾ãã¾ãã
Array.prototype.alertLength = function(){ alert(this.length); };
âããããå¤ãªæ¡å¼µããã¦ããã©ã¤ãã©ãªã使ã£ã¦ããã°ããã¹ã¦ã®é åã®for inã«"alertLength"ãåºç¾ããããã«ãªã£ã¦ãã¾ãããã¡ããArray.prototypeãç¡è¨ç»ã«æ¡å¼µããã®ã¯è¡åãæªãã®ã§ãããããã®ãªã¹ã¯ã«åããã¨ããæå³ã§ãã
ãããã«ãããã¯å±ãªããã§ãã
çç±2: é åè¦ç´ 以å¤ã®ããããã£ã追å ããã¦ãããªã¹ã¯ããããã
çç±1ã¨ä¼¼ã¦ãã¾ãã
é åã¯ãªãã¸ã§ã¯ããªã®ã§ãè¦ç´ 以å¤ã®ããããã£ã追å ã§ãã¦ãã¾ãã¾ãã
var ar = [1, 2, 3]; ar.pet = "cat";
ãããªã¨ããã«ãè¬ã®ãã³ãããã
ãªãããã®ã¨ãã®é åã®lengthã¯ãè¦ç´ ã®ã¤ã³ããã¯ã¹ä¸é + 1 ã®å¤ã«ãªãã¾ãã
console.log(ar.length);
3
é ãæã£ã¦ããããããã£ãpetãã¯ãã¼ã«ã¦ã³ãã§ããã¾ããããã³ã«æ°ã¥ããªãããã
ããããfor inã使ãã¨ã(åå¨ããè¦ç´ æ° + åå¨ããããããã£æ°)åãã«ã¼ããå®è¡ããã¾ãã
for (var n in ar) { console.log(n); }
0 1 2 pet
ï¼ãã³ããã¼ï¼
é åãfor inã§åãæãfor inã®ä¸ã«æ¸ãã®ã¯ãé åã®è¦ç´ ã«å¯¾ãã¦å®è¡ãããå¦çã ã¨æãã¾ããããããé åãªãã¸ã§ã¯ããããããã£ãæã£ã¦ããã¨ãããããã£ã«å¯¾ãã¦ãåæ§ã®å¦çãé©ç¨ããã¾ããã¯ããã¦ããã¯æå³ããå¦çãã¨ãã話ã§ãã
ã¾ããé åè¦ç´ ã¨ããããã£ã¨ã§ãåãããã¦ããå¯è½æ§ãããã¾ããå¦çã«ãã£ã¦ã¯ã¨ã©ã¼ã«ãªãã§ãããã
çç±3: ãµã¤ãã®forã®æ¹ãããã©ã¼ãã³ã¹ããããã
for inããããµã¤ãã®forã®æ¹ããããé«éãªã®ã ããã§ãã
ã¨ãã£ã¦ããescape_artistãããæ¸ãã¦ãã ãã£ãã¨ããããã®å·®ã¯ãã¾ãéè¦ã§ã¯ãªãã¨æãã¾ããã¦ã¼ã¶ã³ã¼ãã§ãã®ã¬ãã«ã®ãã¤ã¯ããã¥ã¼ãã³ã°ãæ¬å½ã«å¿ è¦ãªå ´é¢ã¯ãå°ãªãã®ã§ã¯ãªãã§ããããã
çå2: for inãå¥ã®ãã®ã§ä»£ç¨ããæ¹ãããã®ãï¼ ããããã¨ããããä½ã使ãã°ããã®ãï¼
ä»åº¦ã¯ãé åã§ã¯ãªãããªãã¸ã§ã¯ãã®ããããã£ãindexingããéã®è©±ã§ãã
- ãããã¿ã¤ããã§ã¼ã³ãããã®ã¼ã£ã¦ããããªãã°ãfor inã§ãããã
- ããããå¤æ°ã®ã¹ã³ã¼ããåºããªãã®ãæ°ã«ãããªãã°ãUnderscore.jsãUglifyJSã使ããã
ã¨ããã®ããçãã®ããã§ãã以ä¸ã®æ¸å¿µãç解ããä¸ã§ä½¿ããªãåé¡ãªãã¨ãããã¨ã§ãã
大éã®keyãåæãããã¨ã®æ¸å¿µ
for inã§ãããã¿ã¤ããã§ã¼ã³ãããã®ã¼ã£ãå ´åããã£ããç¶æ¿ã®æ·±ããªãã¸ã§ã¯ããã«ã¼ãããã¨ã大éã®keyãåæãã¦ãã¾ãå¯è½æ§ãããã¾ãããã¡ãããããã©ã¼ãã³ã¹ãå£åãã¾ãã
å ·ä½çã«ã¯ãDOMãã«ã¸ã¥ã¢ã«ã«for inããã¨é ·ããã¨ã«ãªãã¨æãã¦ãããã¾ããã
ã¹ã³ã¼ãã«é¢ããæ¸å¿µ
JavaScriptã¯ãé¢æ°ã使ããã¨ã§ãããã¹ã³ã¼ããåºåããã¨ãã§ãã¾ããã
ãã®ãããfor inã§åãåºãããªãã¸ã§ã¯ããåãåãããã®å¤æ°ã¯ãfor inãæ¸ãããã¹ã³ã¼ãå ã®ã©ãããã§ãã¢ã¯ã»ã¹ã§ãã¦ãã¾ãã¾ãã
ãããåé¿ããããã«ãå種ã©ã¤ãã©ãªã®ãã³ãªé¢æ°ã使ãã®ããªã¹ã¹ã¡ã¨ã®ãã¨ã§ããã
Underscore.jsã®_.eachã_.mapãã¾ããé åã®ä¸èº«ãå¥ã®é åã«ã³ãã¼ãããããªã¨ãã«ã¯ã_.reduceã使ãã°ãã«ã¼ãæã«å®è¡ããå¦çãé¢æ°ã«ã§ããã®ã§ãå¤æ°ãå¤ã«æ¼ãããã«æ¸ã¿ã¾ãã
// 3ã¤ç®ã®å¼æ°ã®é åã¯ãmã®åæå¤ã§ãããæ»ãå¤ _.reduce([1,2,3], function(m, n, i) { m[i] = n; return m; }, []);
ä½è«
ä½è«ã§ãããååã
_.keysã¯ããã¤ãã£ãã®for inã¨éã£ã¦ããããã¿ã¤ããã§ã¼ã³ãããã®ã¼ã£ã¦ã¯è¦ã¦ãããªããã§ããã
ãªã©ã¨æ¸ãã¾ãããããããã¯ãããããã®èãæ¹ãããããã£ãããã§ãã
_.keysã¯ããã®ãªãã¸ã§ã¯ãã®ããããã£ããè¦ãããã¤ãã£ãã®for inãããæ¢ç´¢ç¯å²ãçãã®ã§ã代ç¨ã¨ãã¦ä¸è¶³ã§ã¯ãªããã¨æã£ãã®ã§ãä¸ã®ããã«æ¸ãã¾ãããããããããããæ¤ç´¢å ãèªåèªèº«ã«éå®ããããã«ã使ãéå ·ã ã£ããã§ããã
ããããæãã¦ãã ãã£ãçããããããã¨ããããã¾ã m(_ _)m