简体   繁体   English

为什么将 function 参数作为字符串或引用传递时,“this”会发生变化?

[英]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

jsFiddle . js小提琴

Assuming the expected result is what I expected...假设预期的结果所期望的......

Example 1示例 1

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.它产生了预期的结果。

Example 2示例 2

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.它也产生了预期的结果。

Example 3示例 3

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.它也产生了预期的结果。

Example 4示例 4

this becomes Window in this example.在本例中, this变为Window This is what I am interested in.这是我感兴趣的。

Example 5示例 5

Here the this becomes Window , because c 's parent object is Window .这里this变成Window ,因为c的父 object 是Window

  1. When passing only a reference to a function (eg ab ), will its this always be Window when called with () ?当仅传递对 function (例如ab )的引用时,当使用()调用时,它的this是否总是Window

  2. 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 的属性外,您可以使用callapply

var x = a.b;
x.call(a);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Javascript,在传递函数参数时保留此引用 - Javascript, preserving this reference when passing function argument 在将函数作为参数传递时使用字符串 - use of string when passing function as argument 为什么在JavaScript中将“ 1.0”传递给函数将其更改为“ 1” - Why does passing '1.0' to a function change it to '1' in javascript 函数作为参数传递时,函数在哪里接收输入? - Where does a function receive its input when passing the function as an argument? 将字符串传递给函数是按值复制还是通过引用传递? - Does passing a string to a function copy it by value or pass it by reference? 将字符串参数传递给javascript函数 - Passing a string argument to a javascript function 为什么在将函数作为参数传递时我必须省略括号? - Why do I have to omit parentheses when passing a function as an argument? 为什么将 function 参数与来自 function 的字符串进行比较时返回“未定义”? - Why is 'undefined' returned when comparing an function argument to a string from a function? 为什么更改参数副本会改变参数? - Why does changing a copy of an argument change the argument? 为什么 javascript 在作为参数或索引传入时将数组解析为字符串 - why does javascript parse an array as a string when passed in as an argument or as an index
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM