(define -ayalog '())

括弧に魅せられて道を外した名前のないプログラマ

JavaScriptのArray.prototype.filter()が最高にロックでドハマりした。

jQueryのfilterと全然違って泣きたいと思った。
最初、jQueryと同じだろって思ってこう書いていた。

coffee> array = [1..10]
coffee> array.filter -> @ > 5

みたいな。filterの中の@(this)は評価対象だと思っていた。つまり、この場合だと1~10の値を期待していた。実際は@でグローバル環境を呼んでいた。これでハマッたからドキュメントを探した。

callback is invoked with three arguments:

the value of the element
the index of the element
the Array object being traversed

つまりこういうこと。

coffee> array = [1..10]
coffee> array.filter (x, i, self) -> console.log "#{x}, #{i}, #{self}"
1, 0, 1,2,3,4,5,6,7,8,9,10
2, 1, 1,2,3,4,5,6,7,8,9,10
3, 2, 1,2,3,4,5,6,7,8,9,10
4, 3, 1,2,3,4,5,6,7,8,9,10
5, 4, 1,2,3,4,5,6,7,8,9,10
6, 5, 1,2,3,4,5,6,7,8,9,10
7, 6, 1,2,3,4,5,6,7,8,9,10
8, 7, 1,2,3,4,5,6,7,8,9,10
9, 8, 1,2,3,4,5,6,7,8,9,10
10, 9, 1,2,3,4,5,6,7,8,9,10
[]

つらぽよい。

filter was added to the ECMA-262 standard in the 5th edition; as such it may not be present in all implementations of the standard.

これ読むとECMAの5からぽいし、jQueryより後だと思うのにjQueryに合わせなかったんだとか思ったり思わなかったり。ロックすぎるだろ。。