サンプルコードのエラー? 使い方の問題?

O'Reilly の Firefox 本「Firefox 3 Hacks」および前作の「Firefox Hacks」に関する話題や独自の Hack について

モデレータ: level, Piro, georgei, dynamis

naoshi

サンプルコードのエラー? 使い方の問題?

投稿記事 by naoshi »

本の109ページのサンプルコードの8行目でエラーが発生しました。
http://firefox3hacks.org/source/ch3/20-109.txt

コード: 全て選択

    init : function() {
        var target = this.target;
        this.types.forEach(function(aType) {
            target.events.addListener(aType, target); // ←ココ
        });
    },
エラーの内容。
//エラー: uncaught exception: [Exception... "Could not convert JavaScript argument arg 1 [extIEvents.removeListener]" nsresult: "0x80570009 (NS_ERROR_XPC_BAD_CONVERT_JS)" location: "JS frame :: chrome://helloworld/content/helloworld.js :: anonymous :: line 14" data: no]
forEachの前に var tmp=this として、addLister(aType, tmp) とすることで
期待した動作になっているように見えますが、あっているでしょうか?
よろしくお願いいたします。

# まだFirefoxとJavaScriptをはじめたばかりなので、
# 実行方法などの問題でしたら申し訳ありません
Moderator
記事: 472
登録日時: 2006年10月29日(日) 21:56

Re: サンプルコードのエラー? 使い方の問題?

投稿記事 by »

naoshi さんが書きました:forEachの前に var tmp=this として、addLister(aType, tmp) とすることで
期待した動作になっているように見えますが、あっているでしょうか?
それであっていると思います。自分なら

コード: 全て選択

for (var i in this.types) {
  target.events.addListener(this.types[i], this);
}
とするかな。
naoshi

Re: サンプルコードのエラー? 使い方の問題?

投稿記事 by naoshi »

functionの中にthisを渡すよりすっきりですね。
ありがとうございました。
Piro
Moderator
記事: 92
登録日時: 2007年5月03日(木) 01:11
お住まい: 東京
連絡する:

Re: サンプルコードのエラー? 使い方の問題?

投稿記事 by Piro »

あわわわ。すみません。リファクタリングの時かなんかにやらかしてしまったようです。以下を「正式版」とさせてください。(増刷分からはこちらに差し替えてもらいます)

コード: 全て選択

function FUELEventListener(aTypes, aTarget, aHandler) {
    this.types = aTypes.split(/,\s*|\s+/);
    this.target = aTarget;
    this.handleEvent = aHandler;
}
FUELEventListener.prototype = {
    init : function() {
        this.types.forEach(function(aType) {
            this.target.events.addListener(aType, this);
        }, this);
    },
    destroy : function() {
        this.types.forEach(function(aType) {
            this.target.events.removeListener(aType, this);
        }, this);
        this.types = this.target = this.handleEvent = null;
    }
};
var pref = Application.prefs.get('browser.sessionstore.resume_session_once');
var listener = new FUELEventListener('change', pref,
      function(aEventItem) {
        alert('pref is changed!');
        this.destroy();
      }
    );
listener.init();
返信する