“这个”的使用是指“这个”。 JavaScript 让我感到困惑
使用 JavaScript 时,令人困惑的事情之一是使用 this
时。
var x = {
ele : 'test',
init : function(){
alert(this.ele);
}
}
但是,当处理多个对象时,尤其是 this
的事件上下文发生变化时,跟踪/理解会变得混乱。
因此,如果有人有更好的意见/指南/想法/更好的实践,请分享。另外我想知道使用 this
是否具有任何(性能)优势或什么?
Working with the JavaScript one of the confusing thing is when using this
var x = {
ele : 'test',
init : function(){
alert(this.ele);
}
}
However when dealing with multiple object and especially events context of this
changes and becomes confusing to keep track/understand.
So if anybody has better inputs/guidelines/thoughts/better practices, please share. Also I would like know if using this
gives any (performance) advantage or what?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这与性能无关,而是与访问对象特定实例的属性有关:-
如果您未在函数中使用
this
,则不会显示“test”。实际上,上面的行与:-
当函数执行时,
call
使用中的第一个参数被分配给this
。现在考虑:-
现在您在警报中什么也没有得到。这是因为上面的内容是有效的: -
在浏览器托管的 Javascript 中,
window
对象与全局对象同义。当“原始”调用函数时,this
默认为全局对象。经典的错误是做这样的事情:-
但是这不起作用,因为附加到 onclick 事件的函数是由浏览器使用如下代码调用的:-
因此,当函数运行时
this
是'不是你想的那样。对于这种情况,我通常的解决方案是: -
请注意,
elem = null
是 IE 内存泄漏的解决方法。It's not about performance, it's about accessing a property of a specific instance of an object:-
Would not display 'test' if you hadn't use
this
in the function.Effectively the above line is the same as:-
the first paramater in the use of
call
is assigned tothis
when the function is executed.Now consider:-
Now you get nothing in the alert. This because the above is effectively:-
In browser hosted Javascript the
window
object is synonymous with the global object. When a function is called "in the raw" then thethis
defaults to the global object.The classic error is doing something like this:-
However this doesn't work because the function attached to the onclick event is called by the browser using code like:-
Hence when the function is running
this
isn't what you think it is.My usual solution to this situation is:-
Note the
elem = null
is IE memory leak work-around.这很令人困惑。这取决于您如何调用该函数。 Doug Crockford 在他的书 Javascript, the Good Parts 中写了一篇很好的文章。其要点在于
不,这与性能无关。
It is very confusing. It depends on how you call the function. Doug Crockford did a good write-up in his book Javascript, the Good Parts. The gist of it is in this excellent answer to an otherwise badly formulated question.
And no, it's not about performance.
对我来说,以下指导方针很有帮助:每次看到
this
时都会想到owner
。拥有函数分配到的变量名的对象将成为this
。如果您无法弄清楚谁拥有它,那么this
将是 window.To me, it helped a lot the following guideline: every time you see
this
thinkowner
. The object who owns the variable name to which the function is assigned will become thethis
. If you cannot make sense to who owns it, thenthis
will be window.关于
this
关键字的一篇很好且具有启发性的文章是 this(无双关语)。这篇文章可能会让你明白一些事情,我知道它对我来说也是如此。基本规则是函数内的
this
关键字始终引用函数所有者,理解结果的关键是理解函数何时被引用以及何时< em>已复制。有关示例,请参阅前面提到的文章。A good and enlightening article on the
this
keyword is this (no pun intended). The article may clear things up for you, I know it did for me.The essential rule is that the
this
keyword inside a function always refers to the function owner, and the key to understanding the consequences is understanding when functions are referred and when they are copied. See the beforementioned article for examples.使用
在then之外
你可以在function()内部参考我
use
outside of the
then you can refer to me inside the function()