Underscore.jsã®å ¨ã¡ã½ããã表ã«ã¾ã¨ãã¦ã¿ã
JavaScript Advent Calendar 2011 (フレームワークコース) ã®9æ¥ç®ã§ãã
ãã£ããã®æ©ä¼ã ã£ãã®ã§ããã®ãããæ°ã«ãªã£ã¦ããã©ã触ãæ©ä¼ããªãã£ããUnderscore.jsãããããã¨å¼ã£ã¦ã¿ã¾ããã
é åé¢ä¿ã®ä¾¿å©ã¡ã½ããã®éåã©ã¤ãã©ãªãªã¤ã¡ã¼ã¸ã§ããããã¿ã¤ãã¼ãã¦ã¼ãã£ãªãã£é¢æ°ãæ¡ä»¶å¤æé¢æ°ãªã©ãããããæ©è½ããã£ã¦é¢ç½ãã§ããã
ã¨ãããããã²ã¨éãå®è¡ããªããããã¹ã¦ã®é¢æ°ã触ã£ã¦ã¿ã¾ããã
é常ã«è¦é£ãã¦æ縮ã§ããã以ä¸ã®Googleã¹ãã¬ããã·ã¼ãã«ããããã¨ã¡ã¢ãåã£ã¦ãã£ãã®ã§ãåèã«ãã¦ãã ããã
ééããªã©ããããããã¾ããããèªåèªèº«ç解ããã£ã¦ããªããã説æãããããç®æãããããããã¾ããã
ãã®éã¯ãææããã ããã¨å©ããã¾ããè¶ ããã©ãããã£ãã®ã§ãèªä¸»çãªã¢ãããã¼ãã¯ããã¶ãããã¾ãã...
ãªã³ã¯å : Underscore.jsの機能表
æ©è½è¡¨ã®è¦æ¹
å·¦ã®åããé ã«ã
- ã«ãã´ãª
- ã¡ã½ããå
- æ¸å¼
- 説æ
- aliases
- native methodå¤æ
- åè
- æ¬ä¼¼çãªåï¼Ocamlã£ã½ãï¼
ã¨ãªã£ã¦ãã¾ãã
ååã®èª¬æãããã¨ä»¥ä¸ã®ã¨ããã§ãã
ã«ãã´ãª
Underscore.jsã®ãµã¤ãã©ããã®ã«ãã´ãªã
ã¡ã½ããå
ã¡ã½ããåã
æ¸å¼
å®è¡ä¾ãå¼æ°ãå«ãã¦æ¸ãããã®ã
説æ
èªåã§èãã説æï¼ããã¯ééã£ã¦ããå¯è½æ§ãé«ãç®æã§ãï¼
aliases
å¥åãå®ç¾©ããã¦ãããã®ã¯ãããåæã
native methodå¤æ
å é¨ã§native methodã«å¤æãã¦ãããã®ã¯ãå¤æããã¡ã½ããåã
åè
æ°ã¥ããç¹ãªã©ã
æ¬ä¼¼çãªå
Ocamlãåå¼·ãã¦ããã®ã§ããã風ã«æ¸ãã¦ã¾ããããªãªã¸ãã«ã®åãåæã«ä»ãã¦ã¾ãããåã£ã¦ããééã£ã¦ããã®å¤æãã§ããªãã¬ãã«ã§ããæ¸ãå§ãã¡ãã£ããå¾æ»ãã§ããªããªã£ããããã¨è¨ã£ã¦æ¶ãã®ããã£ãããªãããç§ã®ããã®ã¡ã¢ã¨ããã...
ãã£ã¨ãç¡è¦ãã¦ãã ãããï¼ãããOcamlã«é è©£ã®æ·±ãæ¹ãããã£ããã£ãããå©è¨ããã ããã¨ããããã§ããï¼
ãªãã·ã§ã³å¼æ°[context]ã«ã¤ãã¦
æ¸å¼ã®åã«ãªãã·ã§ã³å¼æ°ã使ã£ã¦ããã¡ã½ãããããã¾ãããå人çã«[context]ã¯ãããã«ããã£ãã®ã§ã解説ãã¾ãã
Collectionã®ã«ãã´ãªã«ã¯ãeach, map, reduce, reduceRight, find, filter, all, any, max, min, sortByã«ãUtilityã®ã«ãã´ãªã«ã¯ãtemplateã«ããã¾ãã
templateã¯ã¡ãã£ã¨éãæå³ã§ä½¿ããã¦ãã¾ããã[context]ã«æå®ãããã®ã¯ãªãã¸ã§ã¯ãã«ãªãã¾ãã
eachã§ä¾ãåºãã¨ä»¥ä¸ã®ãããªæãã§ãã
var messs = ['My', 'name', 'is', 'taiju']; var Blog = function(author) { this.author = author; }; var blog = new Blog('taiju'); _.each(messs, function(mess) { console.log(this.author + ' said: ' + mess); }, blog);
eachã«[context]ã渡ããä¾ã§ããè¦ã¦ãããããã«ãthisã®å¤ã«[context]ã§æå®ãããªãã¸ã§ã¯ããæç¸ããã¾ãã
å é¨ã§thisã®å¤ã使ããããªé¢æ°ã渡ãæã«ã[context]ã使ãã¾ãã
ãã¤ãã£ãã¡ã½ããå¤æã«ã¤ãã¦
JavaScriptãæã¤ã¡ã½ããï¼ä»¥å¾ããã¤ãã£ãã¡ã½ããã¨è¡¨è¨ï¼ã§å¦çãå®ç¾ã§ãããã®ã«é¢ãã¦ã¯ãå é¨ã§ãã¤ãã£ãã¡ã½ããã«å¤æãã¦å®è¡ãã¦ãããã®ãããã¾ãã詳ããã¯ã表ä¸ã®native methodå¤æã®åããåç §ãã ããã
ãªãªã¸ãã«ã®å¦çããããæé©åããããã¤ãã£ãã¡ã½ããã使ãããæ¹ããã¯ãå¹ççã§ãã
eachã¡ã½ããã§ãã³ããåã£ã¦ã¿ã¾ãããeachã¡ã½ããã¯Array.prototype.forEachã使ããç°å¢ã§ã¯ãããã使ãããã«æé©åããã¦ãã¾ãã
eachã¡ã½ããã®ãã³ã - jsdo.it - share JavaScript, HTML5 and CSS
Array Likeãªãã¸ã§ã¯ãã§ã¯ãforEachã¡ã½ããã使ãã¾ããã®ã§ãããã©ã¼ãã³ã¹ãä½ããªã£ã¦ãã¾ããeachã¡ã½ããã¯é åã ãã§ã¯ãªãããªãã¸ã§ã¯ãã«ã使ããã¨ãã§ãã¾ããããªãã¸ã§ã¯ãï¼Arrayãªãã¸ã§ã¯ããé¤ãï¼ããforEachã¡ã½ããã使ãã¾ããã®ã§ãããã©ã¼ãã³ã¹ãä½ããªãã¾ããï¼ä¸è¨ã®ãã³ãã§ã¯å¾®å¦ãªå·®ã§ãããè¦ç´ æ°ãå¤ãã¨ãããããããªããããããã¾ãããArray Likeãªãã¸ã§ã¯ãã¯æãå¹çãæªãã§ãããï¼
ããã©ã¼ãã³ã¹ãæ°ã«ãªãã®ã§ããã°ã_.toArrayã¡ã½ãããªã©ãé©å®å©ç¨ãã¦ããã¤ãã£ãã¡ã½ããã使ãããããã«ããã°ã©ãã³ã°ããæ¹ãè¯ãããªã¨æãã¾ããã¨ã¯è¨ããã©ãã¨ã¯è¨ãã¾ããããããããArray.prototype.forEachãæã£ã¦ããªããã©ã¦ã¶ä½¿ã£ã¦ãæã¯ç¡æå³ã§ããã
ã½ã¼ãæ¸ãªã¹ãã®æé©å
ä¸é¨ãã½ã¼ãæ¸ãªã¹ãã渡ããã¨ã§ã¢ã«ã´ãªãºã ãããå¹çã®è¯ããã®ã«æé©åãããã¡ã½ãããããã¾ãã
å ·ä½çã«ã¯ãuniqã¨indexOfã§ãã
ãããã®ã¡ã½ããã«é åã渡ãéãã½ã¼ãæ¸ã®é åã渡ãã¦ããã¤ã[isSorted]ãªãã·ã§ã³ã«trueã渡ãã¨ããå¹çã®è¯ãã¢ã«ã´ãªãºã ãé¸æããã¾ãã
å ·ä½çã«ã¯uniqã®å ´åãæé©åããã¨ãé åã®èµ°æ»ãä¸åº¦ã§æ¸ã¿ã¾ãï¼æé©åãããªãå ´åã¯ãè¦ç´ ã®æ°ã ãé åå ¨ä½ãèµ°æ»ããï¼ã
indexOfã®å ´åã¯ãæé©åããã¨ãç·å½¢æ¢ç´¢ã§ã¯ãªããäºåæ¢ç´¢ã«ãªãã¾ãã
ãã³ããåã£ã¦ã¿ã¾ããã
[Underscore.js]indexOfã®ãã³ã - jsdo.it - share JavaScript, HTML5 and CSS
indentityã¡ã½ããã«ã¤ãã¦
å¼æ°ããã®ã¾ã¾è¿ãé¢æ°ãè¿ãé¢æ°ã§ãã
é¢æ°åè¨èªã§ã¯ãä¸è¬çã«æçé¢æ°ã¨å¼ã°ãããã®ã§ãã¦ã¼ã¶ã¼ã使ããã¨ã¯ãªãã¨æãã¾ãããé«éé¢æ°ãå©ç¨ããã·ã¼ã³ã«ããã¦ãä½ãä½ç¨ãåã¼ããªãããé¢æ°å ¨ä½ãå£ããããªãé¢æ°ã¨ãããã¨ã§ãå©ç¨ä¾¡å¤ãããã®ã ã¨æãã¾ãã
å®éã«ãå é¨çã«ãiteratorã®ããã©ã«ãå¤ã¨ãã¦ä½¿ãããã·ã¼ã³ãããã¾ãã
OOPã¹ã¿ã¤ã«ã®ããã°ã©ãã³ã°ã«ã¤ãã¦
Underscore.jsã§ã¯ãOOPã¹ã¿ã¤ã«ã®ããã°ã©ãã³ã°ãã§ããããã«ãªã£ã¦ãã¾ãã
ãµã¤ãã«è¼ã£ã¦ãããµã³ãã«ã³ã¼ãã以ä¸ã«ã³ãããã¾ãã
var lyrics = [ {line : 1, words : "I'm a lumberjack and I'm okay"}, {line : 2, words : "I sleep all night and I work all day"}, {line : 3, words : "He's a lumberjack and he's okay"}, {line : 4, words : "He sleeps all night and he works all day"} ]; _(lyrics).chain() .map(function(line) { return line.words.split(' '); }) .flatten() .reduce(function(counts, word) { counts[word] = (counts[word] || 0) + 1; return counts; }, {}).value(); // => {lumberjack : 2, all : 4, night : 2 ... }
_()ã®ä¸ã«ã³ã¬ã¯ã·ã§ã³ãæ¿å ¥ãã¦ããã®ã¾ã¾ã¡ã½ãããã§ã¤ã³ã§å¦çãã¤ãªãã¦ãã¾ããã
_()ã®è¿ãå¤ã¯ãthis._wrappedã«å¼æ°ã®ãªãã¸ã§ã¯ããä»£å ¥ãããã©ããã¼ãªãã¸ã§ã¯ãã§ãããã¦ã¼ã¶ã¼ãUnderscoreãªãã¸ã§ã¯ããæ¡å¼µããç¨éã«ã使ããmixinã¡ã½ããã使ã£ã¦ãwrapperãªãã¸ã§ã¯ãã®prototytpeãªãã¸ã§ã¯ãã«ãUnderscoreãªãã¸ã§ã¯ãã«è¿½å ããã¡ã½ãããå ¨ã¦çµåããã¦ãã¾ãã
_.prototypeã¨wrapper.prototypeã¯å ±éã®åç §ãæã£ã¦ããã®ã§ãã©ã¡ãã®æ¹æ³ã§ãåãã¡ã½ããã使ããããã«ãªã£ã¦ãã¾ãã
å¼æ°ã®é çªã«ã¤ãã¦
Underscore.jsã§ã¯ãã³ã¬ã¯ã·ã§ã³ãé åãæå®ããæ±ãã¡ã½ããã«é¢ãã¦ã第ä¸å¼æ°ãã³ã¬ã¯ã·ã§ã³ãã¾ãã¯é åã«ãªã£ã¦ããã¡ã½ãããã»ã¨ãã©ã§ãã
ããã«ã¯å¼æ°ã®é çªã«ä¸è²«æ§ããã£ã¦ããããããã¨ããå©ç¹ãããä»ãã¡ã½ãããã§ã¼ã³ãå®è¡ã§ããããã«ããããã®ãchainã¡ã½ããã«ããã¦ãéè¦ãªãã¤ã³ãã«ãªã£ã¦ãã¾ãã
åè¿°ããã_()ã®è¿ãå¤ã§ããã©ããã¼ãªãã¸ã§ã¯ãã¯ãchainã¡ã½ãããæã¡ã¾ããchainã¡ã½ãããå®è¡ããã¨ãå é¨ã®chainãã©ã°ãç«ã¡ããã以å¾ãchainãã©ã°ãç«ã£ã¦ããéããã¡ã½ããããã§ã¤ã³ã§ã¤ãªãããã¨ãã§ãã¾ãã
ãã§ã¤ã³åãããã©ããã¼ãªãã¸ã§ã¯ãã¯ãåã¡ã½ããã®å®è¡æããã®ã¡ã½ããã®ç¬¬ä¸å¼æ°ã«ãã©ãããããã³ã¬ã¯ã·ã§ã³ã渡ãå¦çã«ãªã£ã¦ãã¾ãããã®ããããã§ã¤ã³ã§ã¤ãªããã¡ã½ããã®ç¬¬ä¸å¼æ°ã¯ãé¢æ°ãªãã¸ã§ã¯ããã渡ãã°ãããé¢æ°é©ç¨å¾ã®ã³ã¬ã¯ã·ã§ã³ã¯ãã©ãããããã³ã¬ã¯ã·ã§ã³ã«ä¸æ¸ãããã¾ããããã«ãã£ã¦ã次ã ã¨ã¡ã½ãããå®è¡ãã¦ããéç¨ã§ãã©ãããããã³ã¬ã¯ã·ã§ã³ãæ¸ãå¤ãã¦ãããã¨ãã§ãã¾ãã
åçåä»ãã§ãprototypeãã¼ã¹ã®ãã¤ã§ãã©ãã§ãæ¡å¼µãã¦ãããï¼ã¨ããããããè¨èªãªãé°ã§ãããããæè»ãªæ©è½ãæä¾ã§ããã£ã¦ãã¨ã§ãæ¹ãã¦ãJSãããã¨æã次第ã§ãã
ã¾ã¨ã
Underscore.jsã¯ãå人çã«è¶ 好ããããªã©ã¤ãã©ãªã ã¨æã£ã¦ãã¾ããããå¼ã£ã¦ã¿ã¦ããã¯ãè¶ å¥½ããªã©ã¤ãã©ãªã§ããã
é¢æ°ããã°ã©ãã³ã°ã«èå³ããã人ã¯ã好ãã«ãªãå¯è½æ§ãé«ãã¨æãã¾ãã
ä»åã¯Advent Calendarã®ç¸ãã§ãå ¬éæ¥ã決ã¾ã£ã¦ãããããä¸é¨è¨äºã®å 容ã妥åããããå¾ã¾ããã§ããããå¾æ¥ãUnderscore.jsã®ã½ã¼ã¹ã³ã¼ããåç §ããªãããããã¡ãã£ã¨è¸ã¿è¾¼ãã å 容ã®è¨äºãæ¸ããããããªã¨æãã¾ãã
ç¹ã«ã¿ã¤ãã¼ç³»ã®ã¡ã½ãããçµæ§ããªããã¼ã ã£ãããå ¬å¼ã®ãµã³ãã«ã³ã¼ãã§ã¯ã¤ã¡ã¼ã¸ããããªãã£ããããªãã·ã§ã³å¼æ°ã®ä½¿ãæ¹ããããããã¾ãã«ãªã£ããããã®ã§ããªãªã¸ãã«ã®ãµã³ãã«ã³ã¼ããç¨ãã¦è§£èª¬ããè¨äºãããã¨ãããªã¨ãå人çã«ãæãã¾ãã
ç¡é§ã«Underscore.jsã«ã¤ãã¦ã®ç¥èãã¤ãã¦ãã¾ãã¾ããããããããã話ã§ãããããªããã«ã±ä½ãã«ãAdvent Calendarã¯ãããã§ããããã
ä»å¹´åº¦åãã¾ã ã空ãã¦ã¾ãï¼ï¼