[英]Why does `this` change when passing the function argument as string or reference?
Have a look at this:看看这个:
var a = {
b: function() {
console.log(this);
}
}
// Example 1
a.b(); // a
// Example 2
eval('a.b()'); // a
// Example 3
setTimeout('a.b()', 100); // a
// Example 4
setTimeout(a.b, 100); // Window
// Example 5
var c = a.b;
c(); // Window
Assuming the expected result is what I expected...假设预期的结果是我所期望的......
When calling b()
, the property of an Object
, this
becomes the property's Object
, here it's the parent a
.当调用
b()
时, Object
的属性, this
成为属性的Object
,这里是父级a
。 It produces the expected result.它产生了预期的结果。
eval()
is meant to adopt its execution context of where it is called, in this case, window
. eval()
旨在采用其调用位置的执行上下文,在本例中为window
。 It also produces the expected result.它也产生了预期的结果。
When passing a string to setTimeout()
, I'd imagine it is ran through something very similar to eval()
.将字符串传递给
setTimeout()
时,我想它会通过与eval()
非常相似的东西运行。 It too produces the expected result.它也产生了预期的结果。
this
becomes Window
in this example.在本例中,
this
变为Window
。 This is what I am interested in.这是我感兴趣的。
Here the this
becomes Window
, because c
's parent object is Window
.这里
this
变成Window
,因为c
的父 object 是Window
。
When passing only a reference to a function (eg ab
), will its this
always be Window
when called with ()
?当仅传递对 function (例如
ab
)的引用时,当使用()
调用时,它的this
是否总是Window
?
Is the only way to keep its this
as a
to pass it as a string to setTimeout()
/ setInterval()
?将
this
a
字符串传递给setTimeout()
/ setInterval()
的唯一方法是吗?
When passing only a reference to a function (egab), will its this always be Window when called with ()?
当仅传递对 function (egab) 的引用时,当使用 () 调用时,它是否总是 Window?
Yes是的
Is the only way to keep its this as a to pass it as a string to setTimeout() / setInterval()?
将其作为字符串传递给 setTimeout() / setInterval() 的唯一方法是吗?
No. Create a new function instead.否。改为创建一个新的 function。
setTimeout(function() { a.b() }, 100);
Developers are often confused about javascript's this keyword.开发人员经常对 javascript 的this关键字感到困惑。 The most important thing to remember is that it is provided by the call.
要记住的最重要的事情是它是由调用提供的。
In the 4th example:在第四个例子中:
// Example 4 setTimeout(ab, 100); // Window
the first argument is a reference to the function, so it's called without any "parent" object.第一个参数是对 function 的引用,因此在没有任何“父”object 的情况下调用它。 Since the call doesn't provide an object, this is set to
window
.由于调用不提供 object,因此将其设置为
window
。
Your comment on example 5:您对示例 5 的评论:
Here the this becomes Window, because c's parent object is Window.
这里 this 变成 Window,因为 c 的父 object 是 Window。
is not really incorrect.并不是真的不正确。 Because the function call doesn't provide an object to use as
this
, it is set to window
(which is the default when no object is provided). Because the function call doesn't provide an object to use as
this
, it is set to window
(which is the default when no object is provided).
Is the only way to keep its this as a to pass it as a string to setTimeout() / setInterval()?
将其作为字符串传递给 setTimeout() / setInterval() 的唯一方法是吗?
No. Other than calling it as a property of an object, you can use call
or apply
:不。除了将其称为 object 的属性外,您可以使用
call
或apply
:
var x = a.b;
x.call(a);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.