2005-11-30 ========== prototype.js ------------ Reference URI: http://lowreal.net/logs/2005/11/30/1 Written Time: 2005-11-30T18:40:21+09:00 Tags: javacript ajax each 使えないから最新の RC を試したんだけど、思ったより使えない。Event.observe っ て、もうちょっとクロスブラウザに考慮していると思ってた。 軽くテストスクリプト書いてごちゃごちゃやってた [script.aculo.us](http://script.aculo.us) の effects.js を使ってみたかっただけとかなんとか。 Event.observe で function (e) {} とか渡しても IE (Internet Explorer) では e にイベントオブジェクトが入らない。 _observeAndCache: function(element, name, observer, useCapture) { var eEvent = function () { this.type = window.event.type; this.targe t = window.event.srcElement; this.currentTarget = this; this.clientX = window.event.clientX; this.clientY = window .event.clientY; this.pageX = document.body.scrollLeft + window.e vent.clientX; this.pageY = document.body.scrollTop + window.even t.clientY; this.shiftKey = window.event.shiftKey; this.altKe y = window.event.altKey; this.ctrlKey = window.event.ctrl Key; this.which = window.event.keyCode; this.stopPropagat ion = function() { window.event.cancelBubble = true } this.preventDefault = function() { window.event.returnValue = false } } if (!this.observers) t his.observers = []; if (element.addEventListener) { this.observers.push ([element, name, observer, useCapture]); element.addEventListener(name, ob server, useCapture); } else if (element.attachEvent) { this.observers.p ush([element, name, observer, useCapture]); element.attachEvent('on' + nam e, 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; i f (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 とか使うのか。