each 使えないから最新の RC を試したんだけど、思ったより使えない。Event.observe って、もうちょっとクロスブラウザに考慮していると思ってた。
軽くテストスクリプト書いてごちゃごちゃやってた script.aculo.us の effects.js を使ってみたかっただけとかなんとか。
Event.observe で function (e) {}
とか渡しても IE では e にイベントオブジェクトが入らない。
_observeAndCache: function(element, name, observer, useCapture) { var eEvent = function () { this.type = window.event.type; this.target = window.event.srcElement; this.currentTarget = this; this.clientX = window.event.clientX; this.clientY = window.event.clientY; this.pageX = document.body.scrollLeft + window.event.clientX; this.pageY = document.body.scrollTop + window.event.clientY; this.shiftKey = window.event.shiftKey; this.altKey = window.event.altKey; this.ctrlKey = window.event.ctrlKey; this.which = window.event.keyCode; this.stopPropagation = function() { window.event.cancelBubble = true } this.preventDefault = function() { window.event.returnValue = false } } if (!this.observers) this.observers = []; if (element.addEventListener) { this.observers.push([element, name, observer, useCapture]); element.addEventListener(name, observer, useCapture); } else if (element.attachEvent) { this.observers.push([element, name, observer, useCapture]); element.attachEvent('on' + name, function () { observer(new eEvent()); }); } },
みたいに prototype.js を直接書き変えて使ってみた。けど、なんか楽しくない。
なんかわくわくしない。つまらない。
ちなみに prototype.js における each の break, continue の実装は、あらかじめ $break と $continue にオブジェクトを代入しておいて、それを投げるというものだった。なるほど文字列投げるよりこっちのほうがいいな。
[1, 2, 3, 4, 2, 6].collect(function (v, i) { if (v == 2) throw $continue; if (i > 4) throw $break; return v; }); //=> [1, 3, 4]
なんで _each を定義させるんだろうと思っていたけどこれのためだね。_each
は Enumerable.each
からのみ呼び出される。Enumerable
の各メソッドは each
を使用する。
each_with_index 相当がねぇよとか思ったけど、each 自体がその役目を負ってる。[1].each(function (value, index) {})
とかける。
あー Event.element とか使うのか。