CoffeeScriptã使ãã¹ããã使ãããã¹ããï¼
æè¿CoffeeScriptçéã®ããã´ã¹ãã£ã¢(æ»èª)ãè³ããã¦ãããCoffeeScriptã使ãã¹ããã使ãããã¹ããï¼ãã¨ãã話é¡ã«ã¤ãã¦ã¾ã¨ãã¦ã¿ãã
以ä¸ã®è¨äºç´¹ä»ã¯è¶
訳ãã¤è¦ç´ãªã®ã§è©³ããç¥ããã人ã¯å
è¨äºãåç
§ã®ãã¨ã
ãã¨ã®çºç«¯ã¯ãã®è¨äºã
SnackJSã®ä½è
ãCoffeeScriptããã£ã¹ãã
A Case Against Using CoffeeScript by Ryan Florence
ãããã°ã®åé¡
CoffeeScriptãçæããJavaScriptã¯ãã¡ãã¨ãã¦ãããã©ãçµå±ã¯èªåãæ¸ããã³ã¼ããããªãããèªã¿ã«ãããèªåã§ç´æ¥æ¸ããã»ããè¦ãããã
ããã«CoffeeScriptããããã°ããã¯ã¼ã¯ããã¼ã¯å¤§å¤ã ã
- ã¾ãåé¡ãJavaScriptå ã®ã©ãã§çºçããã®ããçªãæ¢ãã(CoffeeScriptã®ã³ã¼ãã¨è¡åä½ã§å¯¾å¿ãã¦ãªããã大å¤ã )
- ããã¦ããã®JavaScriptãç解ãã(èªåãæ¸ããã³ã¼ããããªãããã)
- ç解ããå¾ãåé¡ã®åå ãæ¢ã
- åé¡ã®åå ãææ¡ããå¾ã«ãç´ãã¹ãCoffeeScriptå ã®ã³ã¼ããæ¢ã
- CoffeeScriptãä¿®æ£ãã
- ã³ã³ãã¤ã«ãã(大æµã®å ´åã¯èªååããã¦ããã©ãããã§ãã¹ãããã®1ã¤ã )
- åé¡ã解決ããå ´åã¯ããã§ããããããããã§ãªãå ´åã¯ã»ã»ã»
- ã³ã³ãã¤ã«ãä¸æãè¡ã£ãã®ãçã
- èªåã®äºæ³éãã«ãªã£ã¦ãããJavaScriptã確èªãã(ã¹ã¿ã¼ãã«æ»ã)
æ®éã«JavaScriptãæ¸ãã¦ãããã°ããå ´åã®ã¯ã¼ã¯ããã¼ã¯ãããªãã
- JavaScriptå ã®åé¡ãè¦ã¤ãã(èªåãæ¸ããã³ã¼ãã®ï¼)
- ä¿®æ£ãã
- åé¡ã解決ããå ´åã¯ããã§ããããããããã§ãªãå ´åã¯ã¹ã¿ã¼ãã«æ»ã
ã©ãèãã¦ãèªåã§æ¸ããJavaScriptããããã°ããã»ããããã
ã·ã³ã¿ãã¯ã¹ãã¿ã«ãã
if (five && six && seven) doStuff();
doSomething() if five and six and seven人éã¯è¨èãããã·ã³ãã«ãèªèãããããã®ã ã
ãã®ããã&&ã®ã»ããä½ãæå³ãã¦ããã®ãç解ããããã
ã¯ã³ã©ã¤ãã¼ã¯é åçã ãã©ãæããã
CoffeeScriptã¯ãã¸ã«ã«æãæ¸ããããä¸æã§ã³ã¼ããæ¸ããã¨ãæ¨å¥¨ãã¦ããã
eat food for food in foodsä¸è¦ããããã«è¦ãããããããªé¢¨ã«æ¸ãã¨ã©ããããããªããªãã
wash plate, brush, sink for key, plate of dishes when plate.dirty if meal.status is 'done'æå³ä¸æã ã
CoffeeScriptã¯"bad parts"ãæã£ã¦ãã
ä¸è¦ç¾ããè¨èªã«è¦ãããã©ãããã¹ã¦ãããã§ã¯ãªãã
ããã¯å¥å¦ã«æããã
getUser = (id) -> url = "users/#{id}" dfd = $.ajax url: url format: 'json' method: 'get' url: url promise: dfd.promise()$.ajaxãä»£å ¥ãã¦ããã®ã§ã¯ãªãå®è¡ãã¦ããã®ã ã¨ææ¡ããã®ã¯é£ãããããã«ããCoffeeScriptãç¥ããªã人ã¯æå¾ã®2è¡ã«å°æããã ããã
ãã£ã¨èªåã®æå³éãã«æ¸ãã¨ãããªãã
getUser = (id) -> url = "users/#{id}" dfd = $.ajax url: url format: 'json' method: 'post' return url: url promise: dfd.promise()ãããããã¯CoffeeScriptãã«ãã¼ãããã¨ãã¦ãã"bad parts"ã§ããautomatic semi-colon insertion(ASI)ã§ãã¼ã¹ã¨ã©ã¼ã«ãªãã
return { foo: 'bar', baz: 'qux' } // not returned due to ASIãã¡ããã¢ãã¦(Fat arrow)ã¯ã¢ã³ããã¿ã¼ã³ã
ãã¡ããã¢ãã¦ã¯ã¯ã¼ã«ã«è¦ãããjQueryã使ã£ãã³ã¼ãï¼
var widget = { attach: function () { this.el.bind('click', $.proxy(function (event) { doStuffWithThis(); }, this)); } };ããããªãã
widget = attach: -> el.bind 'click', (event) => doStuffWithThis()ç¡åã³ã¼ã«ããã¯é¢æ°ãé£éããã¦ããã®ã¯ã¢ã³ããã¿ã¼ã³ã®1ã¤ã§ãæããããã¯ã¤ãã¹ãã¼ã¹åé¡ãå¼ãèµ·ããããã¡ããã¢ãã¦ã¯å¿å´ã¸ãããéãæã示ãã¦ããã
ããããCoffeeScriptã§ããæ¸ããã¨ãã§ãããwidget = attach: -> el.bind 'click', $.proxy this, 'handler' handler: (event) -> doStuffWithThis()éè¦ãªãã¯ã¤ãã¹ãã¼ã¹ + ã¹ãã²ãã㣠=== æ»
éè¦ãªãã¯ã¤ãã¹ãã¼ã¹ã¯ã¨ã£ã¦ãã¯ã¼ã«ã ã
ãããã30è¡ã®ãã¡ããã¡ãã«ãã¹ããã¦ããç¡åã³ã¼ã«ããã¯é¢æ°ãçµãããªãjQueryã®ã¡ã½ãããã§ã¤ã³ãããå æ¸ãªæ¡ä»¶æ(if else if if elseif else if unless else)ã¿ãããªé ·ãJavaScriptã§æ¸ã人ãCoffeeScriptãæ¸ããããã£ã¨ã²ã©ãäºã«æãã ããã
CoffeeScriptã¯ç¾ãããããã©ã使ããª
CoffeeScriptãæ¸ãã¦ããã¨ãã¯æ¥½ããããã©ããããæªå¤¢ã«å¤ããã ãããç解ãããã¨ããããã°ãããã¨ãé£ãããªãããã ã
çµå±ããã¯æã ã®ç¾ç¶ã¨åããªãã ããã©ãã
ä¸è¨è¨äºã«å¯¾ããåè«è¨äºã
CoffeeScript is not a language worth learning(CoffeeScriptã¯å¦ã¶ã«å¤ããè¨èªã§ã¯ãªã) by Reg Braithwaite
A Case Against Using CoffeeScriptã®ä¸ã§ãRyan Florenceã¯ä¸æãããªCoffeeScriptãæ¸ã人ã®ããã°ã©ã ã¯JavaScriptã§æ¸ããå ´åãããã²ã©ããªãã¨èªã£ã¦ãããçç´ã«è¨ã£ã¦å½¼ã¯æ£ããããã®ä¸ã®ä¸ã«ã¯æãããåºæ¥ã®çæãããJavaScriptã³ã¼ãããããã¹ã¿ã¼ã¸ã§ã³ãè¨ã£ãããã«ããããããã®ã®90%ã¯ã¯ãºã§ãããããããã90%ã®ã¯ãºãã¯ãºã§ããçç±ã¯CoffeeScriptã«ããã®ã ãããï¼ç§ã¯ããã¯æããªãã
CoffeeScriptã¯JavaScriptã
CoffeeScriptã¯å¦ã¶ã«å¤ããè¨èªã§ã¯ãªãããªããªãCoffeeScriptã¯è¨èªã§ã¯ãªãããã ãCoffeeScriptã¯JavaScriptã ã"CoffeeScriptã§èãã"ã®ã§ã¯ãªãã"JavaScriptã§èãã"ã®ã ã
æããã«ãCoffeeScriptã¯ç°ãªãã·ã³ã¿ãã¯ã¹ãæã£ã¦ãããããããããã¯æ¥µãã¦è¡¨é¢çãªãã®ã ãJavaScriptãè±èªã ã¨ããããCoffeeScriptã¯ãã©ã³ã¹èªã®ãããªä»ã®è¨èªã§ããã¸ã£ãã¤ã«ã³ã»ããã¯ã®ãããªæ¹è¨ã§ããªãããã¯ãã«ã«ãªã¸ã£ã¼ã´ã³ã¿ãããªãã®ã ã
CoffeeScriptã¯ãªããã°ã©ã ãæ¸ãä¸ã§ã®åçãªæ°ããæ¹æ³(ã¢ããã®ãããª)ãããããã¦ããããã®ã§ã¯ãªãããã¹ã¦ã®å¤æã¯ãã¼ã«ã«ãªãã®ã ãCoffeeScriptã®ã¹ãããããè¦ã¦ãããã°ããããä»ã®é¨åã«ã¯ãªãã®å½±é¿ããªãJavaScriptã«å¤æããããã¨ãããã£ã¦ããããã ããã
this.render()ã®ä»£ããã«@render()ãæ¸ãã¨ããããããã¯ãã ã®ç縮è¨æ³ã§ãè¨èªãããªããããæ¸ãã¨
if foo and @get('bar') doThis() doThat()ãããªãã
if (foo && this.get('bar')) { doThis(); doThat(); }ããã®ã©ãã«èºèµ·ã«ãªããã ï¼CoffeeScriptã¯é åå å 表è¨ãç ´å£çä»£å ¥ãç½µããã"ãã¡ããã¢ãã¦"ãªã©æ²¢å±±ã®å¨ç¨ãªå¤æãè¡ãããããã¯ãã¹ã¦JavaScriptã§ã¯ã§ããªããã©CoffeeScriptã ããã§ãã"è¨èªæ©è½"ãããªãã
ç§ã¯ãããã®æ©è½ãå¥ã®è¦ç¹ã§è¦ãããããã¯JavaScriptã®ãã¶ã¤ã³ãã¿ã¼ã³ã ãCoffeeScriptãè¬ãããJavaScriptã«ã³ã³ãã¤ã«ããããã®è¨èªã§ã¯ãªããã¹ã¿ã³ãã¼ããªãã¶ã¤ã³ãã¿ã¼ã³ã«ãã£ãã³ã¼ããçæãã¦ãããJavaScriptã ã¨èãã¦ããããããã«ã¼ããæ¸ãæ¹æ³ã ããããããã©ã«ãå¼æ°ãã¨ãé¢æ°ãæ¸ãæ¹æ³ã ããããthisãæãå®å¤ãæã¤é¢æ°ãæ¸ãæ¹æ³ã ããããã¯ã©ã¹å¿åOOãæ¸ãæ¹æ³ã ããããsuper()ãé¢æ°å ã§å¼ã¶æ¹æ³ã ã
CoffeeScriptãçæããJavaScriptã¯ä¸è²«æ§ãæã¡ãJavaScriptãæã¤ä¸è¬çãªåé¡ãæ¨æºçãªæ¹æ³ã§è§£æ±ºããæ¹æ³ãæã£ã¦ãããä¸çªç´ æ´ãããã®ã¯ãCoffeeScriptã¯åãåé¡ãåãæ¹æ³ã§æ£ç¢ºã«è§£æ±ºãã¦ããããã¨ã ã
æ³åãã¦ããããããJavaã使ã人ã ãJavaScriptãæ¸ãããã©ããªãããå½¼ãã¯ãã¶ã¤ã³ãã¿ã¼ã³ãå©ç¨ããã ãããå½¼ãã¯èªåçã«ãã¶ã¤ã³ãã¿ã¼ã³ã¹ã±ã«ãã³ãçæãã¦ãããIDEã使ãã ããã
class OneTimeWrapper constructor: (@what) -> K: (fn, args...) -> functionalize(fn)(@what, args...) @what T: (fn, args...) -> functionalize(fn)(@what, args...) chain: -> new MonadicWrapper(@what) value: -> @whatIDEã¯èªåçã«ãããæ¡å¼µããã
OneTimeWrapper = (function() { function OneTimeWrapper(what) { this.what = what; } OneTimeWrapper.prototype.K = function() { var args, fn; fn = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : []; functionalize(fn).apply(null, [this.what].concat(__slice.call(args))); return this.what; }; OneTimeWrapper.prototype.T = function() { var args, fn; fn = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : []; return functionalize(fn).apply(null, [this.what].concat(__slice.call(args))); }; OneTimeWrapper.prototype.chain = function() { return new MonadicWrapper(this.what); }; OneTimeWrapper.prototype.value = function() { return this.what; }; return OneTimeWrapper; })();è¬ãããã¨ããã¯ãªããããã¯ç§ãæ¸ãã¹ãJavaScriptã ã
I treat CoffeeScript as a big TextMate snippet that's always editable.â@topfunky
ç§ã«ã¨ã£ã¦ã¯ãCoffeeScriptãæ¸ããã¨ã¯JavaScriptãæ¸ããã¨ã ãçç¥è¨æ³ã§ã¹ã¿ã³ãã¼ããªãã¶ã¤ã³ãã¿ã¼ã³ãæå³ãããã¨ãè£å©ãã¦ããããã¼ã«ãããã ãã ã
CoffeeScriptã¯è¨èªã§ã¯ãªããJavaScriptã®ããã®ã³ã¼ãã£ã³ã°ã¹ã¿ã³ãã¼ãã ã
ç§è¦ãã¾ã¨ãããã"CoffeeScript"ã¯æ°ããããã°ã©ãã³ã°è¨èªã§ã¯ãªããCoffeeScriptã¯JavaScriptãã¹ã¿ã³ãã¼ããªãã¶ã¤ã³ãã¿ã¼ã³ã§æ¸ãããã®çç¥å½¢ã®éåã ãçæãããJavaScriptã¯é«åº¦ã«æé©åãããã¹ãã²ããã£ã§ã¯ãªããJavaScriptã®Good Partsã ã
CoffeeScriptã¯ä»äººãæ¸ããã³ã¼ããèªã¿ããããã¦ããããCoffeeScriptã¦ã¼ã¶ã¼ã¯åãæ¹æ³ã§ã«ã¼ããæ¸ããåãæ¹æ³ã§ã¯ã©ã¹ãæ¸ããç§ãã¡ã¯åããã¿ã¼ã³ã使ãããªããªãCoffeeScriptãããããçæããããã ã
ããã¯Pythonã®éè¦ãªãã¯ã¤ãã¹ãã¼ã¹ã¨åãäºã ããããããå°ããã¤éã£ãããæ¹ã§ã³ã¼ããæ¸ããã¨ã¯æ失ã ãã¹ã¿ã³ãã¼ããªã¤ã³ãã³ãã¼ã·ã§ã³ã§ãã¹ã¿ã³ãã¼ããªOOPã§ãã¹ã¿ã³ãã¼ããªã«ã¼ãã§æ¸ããã¨ã§JavaScriptã¯èªã¿ããããç解ãããããä¿å®ãããããã®ã«ãªãã
ææ³
åã¯å¾è
ã®è¨äºãæ¯æããã
Ryan Florenceã®ãããã°ãããéç¨ãé¢åã ã¨ããææã¯ãã£ã¨ãã ããReg Braithwaiteãè¨ãããã«CoffeeScriptã§JavaScriptãæ¸ããã¨ã«ãã£ã¦ã³ã¼ããå¹³æºåããã¦ãããã¯èªä½ã¯ããããæãã®ã§ã¯ãªããã¨æãã
ããã«CoffeeScriptã¯JavaScriptã®Good Partsã®éåä½ã ã¨ããææãããã£ã¨ãã§ãåã¯CoffeeScriptã使ããã¨ã§ããJavaScriptã®ç解ãæ·±ã¾ã£ãã¨æãã
ã¨ããããã§ãCoffeeScriptã使ããï¼