+ JavaScript の質問用スレッド vol.65 +at HP
+ JavaScript の質問用スレッド vol.65 + - 暇つぶし2ch519:Name_Not_Found
08/06/25 06:48:50
Class内のattachEventに関して質問です。
<html><head><script>
this.eTest = function(){
    this.prop = 100;
}
this.eTest.prototype.showProp = function(){
    alert(this.prop);
}
this.eTest.prototype.setEvent1 = function(){
    document.body.attachEvent("onclick",function(x){return function(){x.showProp()}}(this));
}
this.eTest.prototype.unsetEvent1 = function(){
    document.body.detachEvent("onclick",function(x){return function(){x.showProp()}}(this));
}
this.eTest.prototype.setEvent2 = function(){
    this.savedFunc = function(x){return function(){x.showProp()}}(this);
    document.body.attachEvent("onclick",this.savedFunc);
}
this.eTest.prototype.unsetEvent2 = function(){
    document.body.detachEvent("onclick",this.savedFunc);
}
var inst = new eTest();
</script></head><body>
<input type="button" value="setEvent1" onclick="inst.setEvent1()">
<input type="button" value="unsetEvent1" onclick="inst.unsetEvent1()">
<input type="button" value="setEvent2" onclick="inst.setEvent2()">
<input type="button" value="unsetEvent2" onclick="inst.unsetEvent2()">
</body></html>
1, unsetEvent1でイベント解除できないのはattachしたときの関数オブジェクトとは 別の関数オブジェクトだからという認識でいいんでしょうか?
2, こういった場合setEvent2のようにattachEventに渡す関数オブジェクトを保持しておき、detachEventに同じものを渡すというやり方で問題ありませんか?
3, メモリリークですが、detachした時点でメモリが解放されるということでいいですか?
4, 同じことを実現するのにベターな書き方はありますか?


次ページ
続きを表示
1を表示
最新レス表示
レスジャンプ
類似スレ一覧
スレッドの検索
話題のニュース
おまかせリスト
オプション
しおりを挟む
スレッドに書込
スレッドの一覧
暇つぶし2ch