Drkcore

29 02 2012 coffeescript Tweet

CoffeeScriptのsplatsの挙動がわからないと思ったがやっぱわかった。

コッココッココココココッヒーな脳になってます。

splatsとはこれです。

最大値を返す関数を例にとります。

まずはNode.jsで

> Math.max(1,9,7,4,2,6)
9
> Math.max([1,9,7,4,2,6])
NaN
> Math.max.call(null,1,9,7,4,2,6)
9
> Math.max.apply(null,[1,9,7,4,2,6])
9

これはOKですね。同様にcoffeeでも

coffee> Math.max 1,9,7,4,2,6
9
coffee> Math.max.call @, 1,9,7,4,2,6
9
coffee> Math.max.call null, 1,9,7,4,2,6
9
coffee> Math.max.apply @, [1,9,7,4,2,6]
9
coffee> Math.max.apply null, [1,9,7,4,2,6]
9
coffee> Math.max.apply [1,9,7,4,2,6]
-Infinity
coffee> Math.max.apply()
-Infinity

最後の2つは第一引数にthisもnullも入れてないからですね。

さてsplatsを使います。

coffee> ary = [1,9,7,4,2,6]
coffee> Math.max ary...
9

なんでMath.max @, ary...じゃないのよ?と思ったがエントリ書いてたら理解してしまった。

coffee> Math.max.call @, ary...
9

こういうことね。

ProductName みいつけた!ぬいぐるみM コッシー

セガトイズ / 2623円 ( 2010-04-30 )


About

  • もう5年目(wishlistありマス♡)
  • 最近はPythonとDeepLearning
  • 日本酒自粛中
  • ドラムンベースからミニマルまで
  • ポケモンGOゆるめ

Tag

Python Deep Learning javascript chemoinformatics Emacs sake and more...

Ad

© kzfm 2003-2021