JavaScript 变量解释

发布于 2024-10-21 13:42:53 字数 68 浏览 2 评论 0原文

非常简单的问题...

想知道

“this”变量在javascript中代表什么... 谢谢

very simple question...

would like to know what the

"this" variable represents in javascript...
thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

小伙你站住 2024-10-28 13:42:53

对 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.

陌上青苔 2024-10-28 13:42:53

宽松地说,它代表调用函数时点左边的内容:

// inside of f, this = x
x.f(1, 2, 3)

// inside of f, this = c 
a.b.c.f(1, 2, 3) 

该规则有许多例外。

首先,如果没有点:

// inside of f, this = the global object ("window", if you're in a browser environment)
f(1, 2, 3)

其次,您可以使用方法 call 和/或 apply 显式设置 this 的值:

// Invokes f with this = myVar, not x (arguments 2 an onward are the ordinary arguments)
x.f.call(myVar, 1, 2, 3)

// Invokes f with this = myVar, not x (arguments are passed as an array)
x.f.apply(myVar, [1, 2, 3])

第三,当您使用 new 调用函数时,this 将引用新创建的对象:

// inside of f, this = a new object, not x
new x.f(1, 2, 3)

Loosely speaking, it represents what is to the left of the dot when you invoke the function:

// inside of f, this = x
x.f(1, 2, 3)

// inside of f, this = c 
a.b.c.f(1, 2, 3) 

There are a number of exceptions to the rule.

First, if you have no dot:

// inside of f, this = the global object ("window", if you're in a browser environment)
f(1, 2, 3)

Secondly, you can use the methods call and/or apply to explicitly set the value of this:

// Invokes f with this = myVar, not x (arguments 2 an onward are the ordinary arguments)
x.f.call(myVar, 1, 2, 3)

// Invokes f with this = myVar, not x (arguments are passed as an array)
x.f.apply(myVar, [1, 2, 3])

Third, when you invoke a function using new, this will refer to the newly created object:

// inside of f, this = a new object, not x
new x.f(1, 2, 3)
笑叹一世浮沉 2024-10-28 13:42:53

与任何其他语言一样,JavaScript 中的 this 变量指的是当前对象。
例如:

document.getElementById('link1').onclick = function()
{
 this.href = 'http://google.com';
}

在 onclick 处理程序中,this 将引用您通过 id 获取的 DOMElement。

The this variable in javascript, as in any other language, refers to the current object.
For example:

document.getElementById('link1').onclick = function()
{
 this.href = 'http://google.com';
}

In the onclick handler the this would refer to the DOMElement which you got by id.

画▽骨i 2024-10-28 13:42:53

它是对我们所在函数或范围的当前所有者的引用。

您可以在此处找到更多信息: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

美胚控场 2024-10-28 13:42:53

在 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文