'this',事件处理程序和命名空间 [英] 'this', event handlers, and namespaces

查看:64
本文介绍了'this',事件处理程序和命名空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




考虑以下简化代码,其中Foo用作

''命名空间'':

var Foo = {

doAlert:function(){

alert(''hello'');

},

myEventHandler:function(e){

this.doAlert();

}

}


document.addEventListener(''keydown'',Foo.doAlert,false);

这给我一个错误,因为在回复事件时,

object''this''是文档,因为那是我附加的

事件处理程序。我知道我可以用'Foo'替换''this''但是

我的理解是每次制作时都会受到性能影响

这样的全球参考。而且,由于我正在使用Javascript开发一个大型网页

应用程序,我希望尽可能避免不必要的性能

点击。


任何人都知道在这种情况下该怎么做的公认标准是什么?


谢谢!

杰夫

Hi,

Consider the following simplified code in which Foo is used as a
''namespace'':

var Foo = {
doAlert: function() {
alert(''hello'');
},
myEventHandler: function(e) {
this.doAlert();
}
}

document.addEventListener(''keydown'', Foo.doAlert, false);
This gives me an error because when responding to the event, the
object ''this'' is the document because that''s what I''ve attached the
event handler to. I know I could just replace ''this'' with "Foo.'' but
my understanding is that I take a performance hit every time I make
such a global reference. And, since I''m developing a large web
application in Javascript, I''d like to avoid unnecessary performance
hits when possible.

Anyone know what is the accepted standard of what to do in this case?

Thanks!
Jeff

推荐答案

5月16日上午9:27,Jeff Bigham< jeffrey.big ... @ gmail.comwrote:
On May 16, 9:27 am, Jeff Bigham <jeffrey.big...@gmail.comwrote:




考虑以下简化代码,其中Foo用作

''命名空间'':


var Foo = {

doAlert:function(){

alert(''hello'');

},

myEventHandler:function(e){

this.doAlert();

}


}


document.addEventListener(''keydown'',Foo.doAlert,false);


这给了我一个错误,因为在响应事件时,

对象'这是文档,因为那就是我特地附

事件处理程序。我知道我可以用'Foo'替换''this''但是

我的理解是每次制作时都会受到性能影响

这样的全球参考。而且,由于我正在使用Javascript开发一个大型网页

应用程序,我希望尽可能避免不必要的性能

点击。


任何人都知道在这种情况下该怎么做的公认标准是什么?
Hi,

Consider the following simplified code in which Foo is used as a
''namespace'':

var Foo = {
doAlert: function() {
alert(''hello'');
},
myEventHandler: function(e) {
this.doAlert();
}

}

document.addEventListener(''keydown'', Foo.doAlert, false);

This gives me an error because when responding to the event, the
object ''this'' is the document because that''s what I''ve attached the
event handler to. I know I could just replace ''this'' with "Foo.'' but
my understanding is that I take a performance hit every time I make
such a global reference. And, since I''m developing a large web
application in Javascript, I''d like to avoid unnecessary performance
hits when possible.

Anyone know what is the accepted standard of what to do in this case?



试试这个主题:


主题:JavaScript中的伪命名空间

< URL :
http://groups.google.com.au/group/co...45e485506c4a92


>
>



-

Rob


--
Rob


5月15日,7:27 *下午,Jeff Bigham< jeffrey.big ... @ gmail.comwrote:
On May 15, 7:27*pm, Jeff Bigham <jeffrey.big...@gmail.comwrote:




考虑以下简化代码,其中Foo用作

''命名空间'':


var Foo = {

* doAlert: function(){

* * alert(''hello'');

*},

* myEventHandler:function(e){

* * this.doAlert();

*}


}


document.addEventListener(''keydown'',Foo.doAlert,false);


这给我一个错误,因为在回复活动时,

对象''this''是文档,因为这就是我将

事件处理程序附加到的。 *我知道我可以用'Foo'代替''this''但是

我的理解是我每次制作时都会受到性能打击

这样全球参考。 *而且,由于我正在使用Javascript开发一个大型网页

应用程序,我希望尽可能避免不必要的性能

点击。


任何人都知道在这种情况下该怎么做的公认标准是什么?


谢谢!

Jeff
Hi,

Consider the following simplified code in which Foo is used as a
''namespace'':

var Foo = {
* doAlert: function() {
* * alert(''hello'');
* },
* myEventHandler: function(e) {
* * this.doAlert();
* }

}

document.addEventListener(''keydown'', Foo.doAlert, false);

This gives me an error because when responding to the event, the
object ''this'' is the document because that''s what I''ve attached the
event handler to. *I know I could just replace ''this'' with "Foo.'' but
my understanding is that I take a performance hit every time I make
such a global reference. *And, since I''m developing a large web
application in Javascript, I''d like to avoid unnecessary performance
hits when possible.

Anyone know what is the accepted standard of what to do in this case?

Thanks!
Jeff



我不知道它是否是可以接受的标准,但是你可以做一些像这样的事情:


var Foo = {

* doAlert:function(){

* * alert(''hello'');

*},

* myEventHandler :( function(t){

= t;

返回函数(e){

* * that.doAlert();

};

})(这)

}


我不认为这会避免性能受到打击,而且你可能不应该听我的建议,因为我还是一个位绿色

javascript。另外,我认为正确的解决方案取决于你想要做什么


I don''t know if it''s the accepted standard of what to do, but you
could do something like:

var Foo = {
* doAlert: function() {
* * alert(''hello'');
* },
* myEventHandler: (function(t) {
that = t;
return function(e) {
* * that.doAlert();
};
})(this)
}

I don''t think this would be avoiding a performance hit though, and you
probably shouldn''t listen to my advice as I''m still a bit green on
javascript. Additionally, I think the proper solution depends on what
exactly you''re trying to do.


Jeff Bigham写道:
Jeff Bigham wrote:

考虑以下简化代码,其中Foo用作

''命名空间'':


var Foo = {

doAlert:function(){

alert(''hello'');

},

myEventHandler:function(e){

this.doAlert();

}

}


document.addEventListener(''keydown'',Foo.doAlert,false);
Consider the following simplified code in which Foo is used as a
''namespace'':

var Foo = {
doAlert: function() {
alert(''hello'');
},
myEventHandler: function(e) {
this.doAlert();
}
}

document.addEventListener(''keydown'', Foo.doAlert, false);



可能你打算写


document.addEventListener(''keydown'',Foo.myEventHandler,false);


,因为在Foo.doAlert()中没有this。

Probably you meant to write

document.addEventListener(''keydown'', Foo.myEventHandler, false);

instead, as there is no `this'' in Foo.doAlert().


这给了我一个错误因为在响应事件时,

对象''this''是文档,因为这就是我所附加的事件处理器。
This gives me an error because when responding to the event, the
object ''this'' is the document because that''s what I''ve attached
the event handler to.



_added_ event _listener_ to


这是一个FAQ,解决方案很简单:

document.addEventListener(

''keydown'',

function(e){Foo.myEventHandler(e)},

false);


也就是说,按照惯例,只有属于引用

构造函数的属性名称和常量原始值的标识符才能开始。带有大写字母的

_added_ event _listener_ to

This is a FAQ, and the solution is simple:

document.addEventListener(
''keydown'',
function(e) { Foo.myEventHandler(e) },
false);

That said, by convention only names of properties that refer to
constructors, and identifiers of constant primitive values, should begin
with a capital letter.


我知道我可以用'Foo'替换''this''但我的理解

是我每次制作这样的全局参考时都会受到性能影响。
I know I could just replace ''this'' with "Foo.'' but my understanding
is that I take a performance hit every time I make such a global reference.



这是正确的,因为执行上下文的'this''

值不需要标识符解析。但是,评估循环将非常短暂;这里的主要问题是可维护性:你真的不想做什么

搜索和替换你必须重命名`Foo''(你应该这样做)。

That is correct, as no identifier resolution is required for the `this''
value of the execution context. However, the evaluation loop would be very
short; the main issue here is maintainability: You really do not want to do
search and replace when you have to rename `Foo'' (and you should do that).


而且,由于我正在使用Javascript开发一个大型Web应用程序,我希望在可能的情况下避免不必要的性能命中。
And, since I''m developing a large web application in Javascript, I''d
like to avoid unnecessary performance hits when possible.



不错的想法。

Good thinking, though.


任何人都知道该怎么做的公认标准是什么案件?
Anyone know what is the accepted standard of what to do in this case?



没有关于它的公认标准,只有ECMAScript规范文本

引导的常识及其实现的实际情况。

PointedEars

-

现实主义:HTML 4.01严格

传福音:XHTML 1.0 Strict

madness:XHTML 1.1 as application / xhtml + xml

- Bjoern Hoehrmann

There is no accepted standard about it, only common sense guided by the text
of the ECMAScript Specification and by the reality of its implementations.
PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann


这篇关于'this',事件处理程序和命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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