将类方法附加到事件处理程序 [英] Attach a class method to event handler

查看:66
本文介绍了将类方法附加到事件处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


到现在为止我这样做了:


[html]

< div id =" my_div"><! - content - >< / div>


[simple-js]

MyClass。 prototype.doSomething = function(){};

MyClass.prototype.assignFun = function()

{

var selfObj = this;

document.getElementById(''my_div'')。onclick = function(e)

{

selfObj.doSomethingWith(e || event) ;

}

}


如何避免关闭对象引用?

而MyClass不是单身人士...

解决方案

5月29日上午8:36,Ugo< priv ... @ nospam.itwrote :


大家好,


到现在为止我这样做:


[html]

< div id =" my_div"><! - content - >< / div>


[ simple-js]

MyClass.prototype.doSomethin g = function(){};

MyClass.prototype.assignFun = function()

{

var selfObj = this;

document.getElementById(''my_div'')。onclick = function(e)

{

selfObj.doSomethingWith(e ||事件);

}


}


如何避免关闭对象引用?

而MyClass不是Singleton ...



如果你想避免关闭然后不用selfObj创建一个。


MyClass.prototype.doSomething = function(){};

MyClass.prototype.assignFun = function()

{

document.getElementById(''my_div'')。onclick = function(e)

{

MyClass.prototype.doSomethingWith(e || event) );

}


}


或者甚至


MyClass.prototype.doSomething = function(){};

MyClass.prototype.assignFun = function()

{

document.getElementById (''my_div'')。onclick =

MyClass.prototype.doSomethingWith;

}

思考可能是个错误术语MyClass作为JavaScript

没有课程。


Peter


Peter Michaux写道:


5月29日上午8:36,Ugo< priv ... @nospam.itwrote:


>直到现在我这样做:

[html]
< div id =" my_div"><! - content - >< / div> ;

[simple-js]
MyClass.prototype.doSomething = function(){};
MyClass.prototype.assignFun = function()
{
var selfObj = this;
document.getElementById(''my_div'')。onclick = function(e)
{
selfObj.doSomethingWith(e || event);
}
}

如何避免关闭对象引用?


MyClass.prototype.assignFun =函数(){

的document.getElementById( '' my_div '')。的onclick =(function(o){

返回函数(e){

o.doSomethingWith(e || window.event);

} ;

})(这个);

};


也就是说,你应该避免混合使用DOM Level 0。和DOM Level 2功能在

一个声明中;至少你应该在混合之前进行功能测试。

这样的参考蠕虫[tm]无论如何都容易出错。考虑到这一点(由于部分专有的,显然不可测试的

方法,仍然容易出现错误的b $ b):


MyClass.prototype。 assignFun = function(){

if(isMethod(document," getElementById"))

{

var target = document.getElementById( ''my_div'');

if(target)

{

target.onclick =(function(o){

返回函数(e){

o.doSomethingWith(e || window.event);

};

}) (这);

}

}

};


>而MyClass不是Singleton ...



如果你想避免关闭然后不用selfObj创建一个。


MyClass.prototype.doSomething = function(){};

MyClass.prototype.assignFun = function()

{

document.getElementById(''my_div'')。onclick = function (e)

{

MyClass.prototype.doSomethingWith(e ||)事件);

}

}



虽然这与原始版本不相同,但是`MyClass ''是

绑定的变量然后关闭。


或者甚至


MyClass.prototype.doSomething = function(){};

MyClass.prototype.assignFun = function()

{

document.getElementById(''my_div'')。onclick =

MyClass.prototype.doSomethingWith;

}



情况越来越糟。现在原型对象的方法将被称为

作为全局对象的方法。


思考可能是个错误就MyClass而言作为JavaScript

没有课程。



这至少是正确的。

PointedEars

-

VAR bugRiddenCrashPronePieceOfJunk =(

navigator.userAgent.indexOf( '' MSIE 5')= -1

&安培;!&安培; navigator.userAgent.indexOf( '' Mac'')!= -1

)// Plone,register_function.js:16


5月30日上午10点35分, Thomas''PointedEars''Lahn< PointedE ... @ web.de>

写道:

[...]


也就是说,你应该避免混合DOM Level 0。和DOM Level 2功能在

一个声明中;至少你应该在混合之前进行功能测试。

这样的参考蠕虫[tm]无论如何都容易出错。考虑到这一点(由于部分专有的,显然不可测试的

方法,仍然容易出现错误的b $ b):


MyClass.prototype。 assignFun = function(){

if(isMethod(document," getElementById"))

{

var target = document.getElementById( ''my_div'');

if(target)

{

target.onclick =(function(o){

返回函数(e){

o.doSomethingWith(e || window.event);

};

}) (this);

}



这不会创建对引用的DOM元素的循环引用

按目标?在这一点上应该避免使用:


target = null;


}

};



-

Rob


Hi guys,

until now I make so:

[html]
<div id="my_div"><!-- content --></div>

[simple-js]
MyClass.prototype.doSomething = function( ) {};
MyClass.prototype.assignFun = function( )
{
var selfObj = this;
document.getElementById(''my_div'').onclick = function ( e )
{
selfObj.doSomethingWith( e || event );
}
}

How do I avoid the closure of my object reference?
And MyClass is not a Singleton...

解决方案

On May 29, 8:36 am, Ugo <priv...@nospam.itwrote:

Hi guys,

until now I make so:

[html]
<div id="my_div"><!-- content --></div>

[simple-js]
MyClass.prototype.doSomething = function( ) {};
MyClass.prototype.assignFun = function( )
{
var selfObj = this;
document.getElementById(''my_div'').onclick = function ( e )
{
selfObj.doSomethingWith( e || event );
}

}

How do I avoid the closure of my object reference?
And MyClass is not a Singleton...

if you want to avoid the closure then don''t create one with selfObj.

MyClass.prototype.doSomething = function( ) {};
MyClass.prototype.assignFun = function( )
{
document.getElementById(''my_div'').onclick = function ( e )
{
MyClass.prototype.doSomethingWith( e || event );
}

}

or perhaps even

MyClass.prototype.doSomething = function( ) {};
MyClass.prototype.assignFun = function( )
{
document.getElementById(''my_div'').onclick =
MyClass.prototype.doSomethingWith;
}
It may be a mistake to be thinking in terms of "MyClass" as JavaScript
doesn''t have classes.

Peter


Peter Michaux wrote:

On May 29, 8:36 am, Ugo <priv...@nospam.itwrote:

>until now I make so:

[html]
<div id="my_div"><!-- content --></div>

[simple-js]
MyClass.prototype.doSomething = function( ) {};
MyClass.prototype.assignFun = function( )
{
var selfObj = this;
document.getElementById(''my_div'').onclick = function ( e )
{
selfObj.doSomethingWith( e || event );
}
}

How do I avoid the closure of my object reference?

MyClass.prototype.assignFun = function() {
document.getElementById(''my_div'').onclick = (function(o) {
return function(e) {
o.doSomethingWith(e || window.event);
};
})(this);
};

That said, you should avoid mixing "DOM Level 0" and DOM Level 2 features in
one statement; at least you should feature-test both before you mix them.
Such Reference Worms[tm] are error-prone anyway. Consider this (still
error-prone because of the partially proprietary, apparently untestable
approach):

MyClass.prototype.assignFun = function() {
if (isMethod(document, "getElementById"))
{
var target = document.getElementById(''my_div'');
if (target)
{
target.onclick = (function(o) {
return function(e) {
o.doSomethingWith(e || window.event);
};
})(this);
}
}
};

>And MyClass is not a Singleton...


if you want to avoid the closure then don''t create one with selfObj.

MyClass.prototype.doSomething = function( ) {};
MyClass.prototype.assignFun = function( )
{
document.getElementById(''my_div'').onclick = function ( e )
{
MyClass.prototype.doSomethingWith( e || event );
}
}

While this is not the least equivalent to the original, `MyClass'' is the
bound "variable" of the closure then.

or perhaps even

MyClass.prototype.doSomething = function( ) {};
MyClass.prototype.assignFun = function( )
{
document.getElementById(''my_div'').onclick =
MyClass.prototype.doSomethingWith;
}

It is getting worse. Now the method of the prototype object will be called
as a method of the Global Object.

It may be a mistake to be thinking in terms of "MyClass" as JavaScript
doesn''t have classes.

That, at least, is correct.
PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf(''MSIE 5'') != -1
&& navigator.userAgent.indexOf(''Mac'') != -1
) // Plone, register_function.js:16


On May 30, 10:35 am, Thomas ''PointedEars'' Lahn <PointedE...@web.de>
wrote:
[...]

That said, you should avoid mixing "DOM Level 0" and DOM Level 2 features in
one statement; at least you should feature-test both before you mix them.
Such Reference Worms[tm] are error-prone anyway. Consider this (still
error-prone because of the partially proprietary, apparently untestable
approach):

MyClass.prototype.assignFun = function() {
if (isMethod(document, "getElementById"))
{
var target = document.getElementById(''my_div'');
if (target)
{
target.onclick = (function(o) {
return function(e) {
o.doSomethingWith(e || window.event);
};
})(this);
}

Doesn''t that create a circular reference to the DOM element referenced
by target? Should that be avoided by using, at this point:

target = null;

}
};


--
Rob


这篇关于将类方法附加到事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆