JavaScript 变量解释
非常简单的问题...
想知道
“this”变量在javascript中代表什么... 谢谢
very simple question...
would like to know what the
"this" variable represents in javascript...
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
对 quirksmode.org 的解释可能是一个好的开始。
还有一个 艾伦的很好的答案Storm 在 stackoverflow 上。
The explanation on quirksmode.org might be a good start.
There is also a nice answer by Alan Storm here on stackoverflow.
宽松地说,它代表调用函数时点左边的内容:
该规则有许多例外。
首先,如果没有点:
其次,您可以使用方法
call
和/或apply
显式设置this
的值:第三,当您使用
new
调用函数时,this
将引用新创建的对象:Loosely speaking, it represents what is to the left of the dot when you invoke the function:
There are a number of exceptions to the rule.
First, if you have no dot:
Secondly, you can use the methods
call
and/orapply
to explicitly set the value ofthis
:Third, when you invoke a function using
new
,this
will refer to the newly created object:与任何其他语言一样,JavaScript 中的
this
变量指的是当前对象。例如:
在 onclick 处理程序中,this 将引用您通过 id 获取的 DOMElement。
The
this
variable in javascript, as in any other language, refers to the current object.For example:
In the onclick handler the this would refer to the DOMElement which you got by id.
它是对我们所在函数或范围的当前所有者的引用。
您可以在此处找到更多信息:http://www.quirksmode.org/js/this.html
It's a reference to the current owner of the function or scope we're in.
You can find more info here : http://www.quirksmode.org/js/this.html
在 JavaScript 中,this 始终指代我们正在执行的函数的“所有者”,或者更确切地说,指代函数所属的对象。
请查看以下链接以获取更多说明。
http://www.quirksmode.org/js/this.html
In JavaScript this always refers to the “owner” of the function we're executing, or rather, to the object that a function is a method of.
Check the below link for more explanation.
http://www.quirksmode.org/js/this.html