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 の 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 とか使うのか。
Inserted at 2005-11-30T21:23:01+09:00
Trackback URI: http://lowreal.net/logs/2005/11/30/1.trackback
prototype.js .inspect $H()
- Reference URI
- http://lowreal.net/logs/2005/11/30/2
- Written Time
- 2005-12-01T02:49:39+09:00
- Tags
-
- javascript
- prototype
$H() と inspect() の組み合わせが微妙に便利だ。普通の object って toString() しても [object Object] とかになって中身がわからんから、$H(obj).inspect() とかやると中身が見れて便利。
Object.prototype.p = function () {
var t = Object.inspect(this);
if (t == "[object Object]")
t = $H(this).inspect().replace(/^#<Hash/, "#<Object");
if (navigator.userAgent.match(/Firefox/)) {
window.dump(t + "\n");
} else {
window.status = t;
}
return this;
};
({aa:"aabb"}).p().aa.p().replace(/^a/, "b").p();
//=> #<Object:{'aa': 'aa'}>
// 'aabb'
// 'babb'
Trackback URI: http://lowreal.net/logs/2005/11/30/2.trackback
NULL
Generated with Taglibro
この日記は Taglibro と呼ばれる XML ベースの XSLT をテンプレートとして使ったシステムを使っています。現在の Taglibro は Ruby, ruby-xslt, libxml-ruby による実装です。ソースコードはとりあえず公開していません。
Comments (0)