<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="/xml.xsl" type="text/xsl"?><feed xmlns="http://www.w3.org/2005/Atom">
  <title>nulog, NULL::something : out of the headphone &gt; 2005 &gt; November &gt; 30</title>
  <link href="http://lowreal.net/logs/2005/11/30"/>
  <icon>http://lowreal.net/img/banner.png</icon>
  <link rel="self" type="application/atom+xml" href="http://lowreal.net/logs/2005/11/30.atom"/>
  <link rel="alternate" type="application/xhtml+xml" href="http://lowreal.net/logs/2005/11/30.xhtml"/>
  <updated>2005-12-01T02:49:39+09:00</updated>
  <author>
    <name>cho45(砂糖)</name>
  </author>
  <id>http://lowreal.net/2005/11/30</id>
  <entry>
    <title>prototype.js</title>
    <link rel="alternate" type="text/html" href="http://lowreal.net/logs/2005/11/30/1.html"/>
    <link rel="alternate" type="application/xml+xhtml" href="http://lowreal.net/logs/2005/11/30/1.xhtml"/>
    <updated>2005-11-30T18:40:21+09:00</updated>
    <published>2005-11-30T18:40:21+09:00</published>
    <id>http://lowreal.net/2005/11/30/1</id>
    <category term="javacript"/>
    <category term="ajax"/>
    <content type="xhtml" xml:base="http://lowreal.net/">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>each 使えないから最新の RC を試したんだけど、思ったより使えない。Event.observe って、もうちょっとクロスブラウザに考慮していると思ってた。</p>
        <p>軽くテストスクリプト書いてごちゃごちゃやってた <a href="http://script.aculo.us">script.aculo.us</a> の effects.js を使ってみたかっただけとかなんとか。</p>
        <p>Event.observe で <code>function (e) {}</code> とか渡しても <abbr title="Internet Explorer">IE</abbr> では e にイベントオブジェクトが入らない。</p>
        <pre class="ECMAScript">
_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());
        });
    }
},</pre>
        <p>みたいに prototype.js を直接書き変えて使ってみた。けど、なんか楽しくない。</p>
        <p>なんかわくわくしない。つまらない。</p>
        <p>ちなみに prototype.js における each の break, continue の実装は、あらかじめ <var>$break</var> と <var>$continue</var> にオブジェクトを代入しておいて、それを投げるというものだった。なるほど文字列投げるよりこっちのほうがいいな。</p>
        <pre class="ECMAScript">[1, 2, 3, 4, 2, 6].collect(function (v, i) {
    if (v == 2) throw $continue;
    if (i &gt; 4) throw $break;
    return v;
}); //=&gt; [1, 3, 4]</pre>
        <p>なんで _each を定義させるんだろうと思っていたけどこれのためだね。<code>_each</code> は <code>Enumerable.each</code> からのみ呼び出される。<code>Enumerable</code> の各メソッドは <code>each</code> を使用する。</p>
        <p>each_with_index 相当がねぇよとか思ったけど、each 自体がその役目を負ってる。<code class="ECMAScript">[1].each(function (value, index) {})</code> とかける。</p>
        <ins datetime="2005-11-30T21:23:01+09:00">
          <p>あー Event.element とか使うのか。</p>
        </ins>
      </div>
    </content>
  </entry>
  <entry>
    <title>prototype.js .inspect $H()</title>
    <link rel="alternate" type="text/html" href="http://lowreal.net/logs/2005/11/30/2.html"/>
    <link rel="alternate" type="application/xml+xhtml" href="http://lowreal.net/logs/2005/11/30/2.xhtml"/>
    <updated>2005-12-01T02:49:39+09:00</updated>
    <published>2005-12-01T02:49:39+09:00</published>
    <id>http://lowreal.net/2005/11/30/2</id>
    <category term="javascript"/>
    <category term="prototype"/>
    <content type="xhtml" xml:base="http://lowreal.net/">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p><code>$H()</code> と <code>inspect()</code> の組み合わせが微妙に便利だ。普通の object って <code>toString()</code> しても <code>[object Object]</code> とかになって中身がわからんから、<code>$H(obj).inspect()</code> とかやると中身が見れて便利。</p>
        <pre class="ECMAScript">Object.prototype.p = function () {
    var t = Object.inspect(this);
    if (t == "[object Object]")
        t = $H(this).inspect().replace(/^#&lt;Hash/, "#&lt;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();
//=&gt; #&lt;Object:{'aa': 'aa'}&gt;
//   'aabb'
//   'babb'
</pre>
      </div>
    </content>
  </entry>
</feed>
