我不知道';我不理解JavaScript中call()方法的要点

我不知道';我不理解JavaScript中call()方法的要点,javascript,methods,Javascript,Methods,请看下面的代码并解释:我做错了什么 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 w

请看下面的代码并解释:我做错了什么

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? 函数doStuff(a,b){ 返回a+b+1; } var myContext={ c:1, d:3 }; //myContext=这个(结果=>5) 调用(myContext,myContext.c,myContext.d) // ... 那么为什么下面的方法不起作用呢?(结果=>NaN) 调用(myContext,this.c,this.d) //要使上述工作正常,我必须将“this”替换为“myContext”(结果=>5)。。。 调用(myContext,myContext.c,myContext.d) //…这和…没什么不同。。。 doStuff(myContext.c、myContext.d) //…那么call()方法的意义是什么?
我很胖吗?

调用
的主要目的是在函数中设置
这个
的值。由于您的
doStuff
没有在函数中使用
this
,因此对其使用
call
是毫无意义的

下面是一个重要的例子:

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

那么为什么下面的方法不起作用呢?(结果=>NaN)

doStuff.call(myContext,this.c,this.d)


因为在调用
doStuff
之前,使用当前的
this
值来计算
this.c
。您如何调用该代码,
是(在松散模式下)全局对象(浏览器上的窗口对象),它可能没有
c
d
属性。(在严格模式下,再次假设您如何调用它,您会得到一个异常,因为
未定义的
,您无法从
未定义的
中检索属性)

调用
的要点是在函数中设置
的值。由于您的
doStuff
没有在函数中使用
this
,因此对其使用
call
是毫无意义的

下面是一个重要的例子:

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

那么为什么下面的方法不起作用呢?(结果=>NaN)

doStuff.call(myContext,this.c,this.d)

因为在调用
doStuff
之前,使用当前的
this
值来计算
this.c
。您如何调用该代码,
是(在松散模式下)全局对象(浏览器上的窗口对象),它可能没有
c
d
属性。(在严格模式下,再次假设您如何调用它,您将得到一个例外,因为
未定义的
,并且您无法从
未定义的
中检索属性)


.call
.apply
是函数方法,它们“操纵”函数内部的
含义

doStuff.call(myContext,myContext.c,myContext.d)
: 这里您已经将
myContext
设置为
doStuff
函数的上下文 您可以使用
this
在内部引用它, 您已经向它传递了两个参数:myContext.c,myContext.d, 它按照你的意图工作


doStuff.call(myContext,this.c,this.d)
: 同样,
myContext
doStuff()的上下文
但是您已经通过了
.c
.d
指向的属性 在案例中它出现的上下文中(全局对象、窗口)。 因此,
doStuff
的上下文是myContext,参数是2
未定义的
s, 因为在调用函数的上下文中,
this===window
, 您正在将
全局
.c
.d
属性传递到函数中。 您实际得到的结果是:
returnundefined+undefined+1(NaN)


如果您这样重新定义“doStuff”:

function doStuff () {

  return this.a + this.b + 1;

  // here it looks whatever this is set to 
  // by `.call()` or `.apply()` methods

}
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'
这样称呼它:

function doStuff () {

  return this.a + this.b + 1;

  // here it looks whatever this is set to 
  // by `.call()` or `.apply()` methods

}
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'

.call
.apply
是函数方法,它们“操纵”函数内部的
含义

doStuff.call(myContext,myContext.c,myContext.d)
: 这里您已经将
myContext
设置为
doStuff
函数的上下文 您可以使用
this
在内部引用它, 您已经向它传递了两个参数:myContext.c,myContext.d, 它按照你的意图工作


doStuff.call(myContext,this.c,this.d)
: 同样,
myContext
doStuff()的上下文
但是您已经通过了
.c
.d
指向的属性 在案例中它出现的上下文中(全局对象、窗口)。 因此,
doStuff
的上下文是myContext,参数是2
未定义的
s, 因为在调用函数的上下文中,
this===window
, 您正在将
全局
.c
.d
属性传递到函数中。 您实际得到的结果是:
returnundefined+undefined+1(NaN)


如果您这样重新定义“doStuff”:

function doStuff () {

  return this.a + this.b + 1;

  // here it looks whatever this is set to 
  // by `.call()` or `.apply()` methods

}
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'
这样称呼它:

function doStuff () {

  return this.a + this.b + 1;

  // here it looks whatever this is set to 
  // by `.call()` or `.apply()` methods

}
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'

Function.prototype.call()
在函数内部设置
this
,因此如果您有
函数doStuff(){返回this.a+this.b+1}
您可以执行
doStuff.call(myContext)函数的这是一个作为其一部分的参数,它不是“上下文”本身。
function.prototype.call()
在函数内部设置
this
,因此如果您有
function doStuff(){返回this.A+this.b+1}
您可以执行
doStuff.call(myContext)函数的这是一个参数,它是函数的一部分,它不是“上下文”本身。它现在有意义了。干杯,现在说得通了。干杯