[英]I don't understand the point of the call() method in JavaScript
Please look at the code below and explain: what am I doing wrong? 请查看下面的代码并说明:我在做什么错?
function doStuff(a, b){ return a + b + 1 ; } var myContext = { c: 1, d: 3 }; // myContext = this (result => 5) doStuff.call(myContext,myContext.c,myContext.d) // ... so why doesn't the below work? (result => NaN) doStuff.call(myContext,this.c,this.d) // To make the above work, i must replace "this" with "myContext" (result => 5)... doStuff.call(myContext,myContext.c,myContext.d) // ...which is no different to... doStuff(myContext.c,myContext.d) // ...so what was the point of call() method?
Am I being thick? 我很胖吗?
The main point of call
is to set the value of this
within the function. 主要点call
是设置的值this
函数中 。 Since your doStuff
doesn't use this
within the function, using call
with it is pointless. 由于doStuff
在函数中未使用this
功能,因此对其进行call
是没有意义的。
Here's an example where it matters: 这是一个重要的示例:
function doStuff(a, b) {
return this.sum(a, b);
}
var obj = {
sum: function(a, b) {
return a + b;
}
};
console.log(doStuff.call(obj, 3, 4)); // 7, because `obj` has a `sum` property
console.log(doStuff(3, 4)); // Fails with an error that `this.sum` is not a function
so why doesn't the below work? 那为什么下面的方法不起作用? (result => NaN) (结果=> NaN)
doStuff.call(myContext,this.c,this.d)
Because this.c
is evaluated before the call to doStuff
, using whatever the current this
value is. 由于this.c
被调用之前评估doStuff
,使用任何当前this
值。 How you're probably calling that code, this
is (in loose mode) the global object (the window object, on browsers), which probably doesn't have a c
or d
property. 您可能如何调用该代码, this
是(在松散模式下)全局对象(浏览器上的window对象),它可能没有c
或d
属性。 (In strict mode, again making assumptions about how you're calling that, you'd get an exception, because this
is undefined
and you can't retrieve properties from undefined
.) (在严格模式下,再次假设如何调用它,将会得到一个异常,因为this
是undefined
,因此无法从undefined
检索属性。)
.call
and .apply
are Function methods that 'manipulate' what this
means inside a function. .call
和.apply
是“操纵”什么功能的方法this
意味着函数内。
doStuff.call(myContext, myContext.c, myContext.d)
: here you've set myContext
as context for doStuff
function and you may refer to it inside by using this
, and you've passed two arguments to it: myContext.c, myContext.d, it works as you've intended... doStuff.call(myContext, myContext.c, myContext.d)
:在这里,您已将myContext
设置为doStuff
函数的上下文,并且可以通过使用this
来引用它,并且已经向其传递了两个参数:myContext.c ,myContext.d,它按您的预期工作...
doStuff.call(myContext, this.c, this.d)
: again myContext
is context for doStuff()
but you've passed .c
and .d
properties of what this
points to in context in which it appears (global object, window) in your case. doStuff.call(myContext, this.c, this.d)
再次myContext
是上下文doStuff()
但你已经通过了.c
和.d
的哪些属性this
其中似乎指向上下文(全局对象,窗口)。 So doStuff
's context is myContext , and parameters are 2 undefined
s, because this === window
in context where you are calling the function, and you are passing .c
and .d
properties of global
into the function. 所以doStuff
的上下文是myContext ,参数是2个undefined
s,因为在您调用函数的上下文中this === window
,并且正在将global
.c
和.d
属性传递到函数中。 you are actually getting this: return undefined + undefined + 1;
您实际上正在得到: return undefined + undefined + 1;
(NaN) (NAN)
if you redefine 'doStuff' like this: 如果您这样重新定义“ doStuff”:
function doStuff () {
return this.a + this.b + 1;
// here it looks whatever this is set to
// by `.call()` or `.apply()` methods
}
and call it like this: 并这样称呼它:
var sum = doStuff.call(myContext);
// notice that using `.call` here
// means that 'myContext' is manualy set
// as `this` value inside the function above
// and is refered to by dynamic variable 'this'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.