使用“this”访问全局属性函数内的关键字
我确实知道,在 javascript 中,当您在函数内使用 "this"
关键字时,"this"
将根据 Quirksmode 网站。因此,当我们有一个函数并在其中使用 "this"
时,"this"
指的是全局(窗口)对象。
我对 "this"
的工作原理有点困惑,例如在下面的代码中,"this"
应该能够解析 x
因为 x 几乎是全局对象(在本例中为窗口)的属性。但在本例中,this.x
会提醒 "undefined"
而不是 x
值。
var x = "Global";
function foo(){
alert(this.x); //undefined
};
foo();
然后我也尝试了其他一些事情:
function bar(){
function foo(){
alert(this); //[Object DOMWindow]
};
foo();
};
bar();
如果我的理解是正确的,那么在第二种情况下 'this'
应该引用 bar()
因为它是 的所有者>foo()
,但为什么它仍然引用全局对象呢?
有人可以解释关于“this”关键字的正确理论是什么吗?
I do know that in javascript, when you use "this"
keyword inside a function, then "this"
would refer to the 'owner' of that function according to Quirksmode website. Therefore when we have a function and we use "this"
inside it, then "this"
refers to the global (window) object.
I am a little confused on how "this"
works, for example in the code below, "this"
then should be able to resolve x
since x
is pretty much a property of global object (in this case window). But this.x
in this case alerts "undefined"
instead of the x
value.
var x = "Global";
function foo(){
alert(this.x); //undefined
};
foo();
I then tried some other things too:
function bar(){
function foo(){
alert(this); //[Object DOMWindow]
};
foo();
};
bar();
If my understanding is correct, then 'this'
should refer to bar()
in that second case since it is the owner of foo()
, but why is that it instead still refers to the global object?
Can someone explain what is the correct theory regarding "this" keyword?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你搞错了方向。
this
的值取决于函数的调用方式,而不是函数的定义方式。window.foo()
那么(在 foo 内)this
将是window
bar.foo()< /code> 那么
this
将是bar
(尽管您需要复制foo
所以它是bar
的属性首先)baz.bar.foo()
那么this
将是bar
(你只能通过this
获取父对象)foo.call(bar) 那么
this
也将是bar
因为call
可以让你覆盖this
new foo ()
那么this
将是正在创建的新对象默认对象是
window
,因此如果您只调用foo()
,那么这与window.foo()
相同代码>.函数定义的范围并不重要。
You've got the wrong end of the stick. The value of
this
depends on how the function is called, not how it is defined.window.foo()
then (inside foo)this
will bewindow
bar.foo()
thenthis
will bebar
(although you need to copyfoo
so it is a property ofbar
first)baz.bar.foo()
thenthis
will bebar
(you only ever get the parent object viathis
)foo.call(bar)
thenthis
will also bebar
ascall
lets you overridethis
new foo()
thenthis
will be the new object being createdThe default object is
window
, so if you just callfoo()
then that is the same aswindow.foo()
.It doesn't matter what scope the function is defined in.
总结你的问题,你问为什么在你的第一个代码片段中,
this.x
是未定义
:它根本没有意义,
this
value 应引用全局对象 - 如果您的代码处于严格模式,您将收到TypeError
,因为this
本身将是undefined
-.我认为
this.x
可能是未定义
的唯一原因是x
的变量声明是在函数内进行的。检查以下两个示例: 1 和 2,这是完全相同的代码,不同之处在于第二个代码被包装在
onload
事件处理程序中,因此x
变量没有存在于全局范围内(window.x
是未定义
)...Summarizing your question, you ask why in your first snippet,
this.x
isundefined
:It doesn't make sense at all, the
this
value should refer to the global object -if your code were on strict mode, you would get aTypeError
, sincethis
by itself would beundefined
-.The only way I think where
this.x
could beundefined
, is in the case that the variable declaration ofx
was made within a function.Check the two following examples: 1 and 2, it's exactly the same code, the difference is that the second one, the code is wrapped in an
onload
event handler, so thex
variable doesn't exist in the global scope (window.x
isundefined
)...是的,
this
始终是正在执行的函数的所有者,有关此主题的最佳答案是/how-does-this-keyword-work-within-a-javascript-object-literal/134149#134149">关于
bar()
,它是一个独立的函数,this
将绑定到“全局”对象,如上面链接的答案中所述,您的情况是DOM窗口
Yes,
this
is always the owner of the function being executed and the best answer about this subject is more than you ever wanted to know aboutthis
As for the
bar()
, it is a standalone function andthis
will be bound to the "global" object as described in the answer linked above, which is your case is theDOMWindow
如果您确实想了解
这是如何工作的
,请阅读ECMAscript 262 规范 从10.3 执行上下文
部分开始。这是第 10.4.3 节中的内容:
If you really wish to learn how
this
works, then read the ECMAscript 262 specs from the section10.3 Execution Context
and onwards.Here is what it says in section 10.4.3: