嵌套功能& “这个” JavaScript中的关键字
此
关键字始终引用包含该函数的方法的对象。
"The this
keyword always refers to the object that the containing function is a method of."
很棒,听起来很简单,但这就是我想知道的......
Great, sounds simple enough, but here's what I'm wondering about...
例如:
function func1() {
function func2() {
alert(this == window); // true
}
func2();
alert(this == window); // true
}
func1.func3 = function () {
alert(this == window); // false
alert(this == func1); // true
};
func1();
func1.func3();
现在,因为 func1
实际上是一种方法全局(窗口
)对象(分配给属性 func1
的函数对象) 全局对象)这个
里面的 func1
指的是全局对象,并且因为 func3
是一个 func1
的函数对象的方法,所以此
里面 func3
是指 func1
的函数对象。
Now, since func1
is actually a method of the global (window
) object (a function object assigned to the property func1
of the global object) it makes sense that this
inside func1
refers to the global object, and since func3
is a method of func1
's function object it makes sense that this
inside func3
refers to func1
's function object.
困扰我的是 func2
。我知道嵌套函数中的这个
也应该引用全局对象,但是我不知道为什么因为 func2
不是全局对象的方法。据我所知(这是我可能完全错误的部分) func2
是 func1
的方法的调用(激活/变量)对象。现在,如果我是对的(我不确定我是),那么这个
里面的 func2
参考 func1
的调用对象而不是全局对象?
The thing that bothers me is func2
. I know that this
inside a nested function is also supposed to reference the global object, but I'm not sure why since func2
is NOT a method of the global object. As far as I understand (and this is the part I might be completely wrong about) func2
is a method of func1
's call (activation / variable) object. Now, if I'm right about this (and I'm not sure that I am) then shouldn't this
inside func2
refer to func1
's call object instead of the global object?
所以,我想我的问题是:嵌套函数是嵌套函数的调用(激活)对象的方法,如果是,则不应该这个
引用该调用对象而不是 global 对象?
So, I guess my question would be: Is a nested function a method of the call (activation) object of the function it is nested in, and if so, shouldn't this
refer to that call object instead the global object?
this关键字始终引用包含函数的方法的对象。
The this keyword always refers to the object that the containing function is a method of.
否。不幸的是,这并不容易。 此$ c的文档MDN上的$ c>关键字
提供了一个很好的概述。当函数作为方法 on 被调用时,它被设置为对象,但还有其他可能性。默认情况下,此
是 undefined
,当它被调用时没有任何特殊情况,就像你使用 func1
和 func2
。对于草率(非严格)模式函数 undefined
(和 null
)不使用,在这种情况下,这个
确实指向它们的全局对象(窗口
) - 你正在观察的内容。
No. Unfortunately, it is not easy as that. The documentation of the this
keyword at MDN gives a good overview. It is set to the object when the function is called as a method on it, but there are other possibilies. The default is that this
is undefined
when it is called without anything special, like you do with func1
and func2
. For sloppy (non-strict) mode functions undefined
(and null
) are not used though, this
does point to the global object (window
in browsers) for them in that case - what you are observing.
但当函数作为构造函数(带有 new
关键字)或事件调用时,它也可能指向新鲜对象实例用作处理程序时的目标(如DOM元素)。最后但并非最不重要的是,它可以通过调用
手动设置, apply
或 bind
...
But it could also point to fresh object instances when the function is called as a constructor (with the new
keyword), or to an event target (like a DOM element) when used as a handler. Last, but not least, it could be set manually with call
, apply
or bind
…
此
与嵌套无关。嵌套函数声明/表达式仅影响变量的范围(隐私,可用性)。虽然函数的变量范围永远不会改变,但$ c>这个的值在每次调用时都可能不同 - 它更像是一个额外的参数。
this
has nothing to do with nesting. Nesting function declarations/expressions only affects the scope ("privacy", availability) of variables. While the variable scope of a function never changes, the value of this
can be different on every invocation - it is more like an extra argument.