Javascript Function.prototype.call和Function.prototype.apply仅使用一个参数

Javascript Function.prototype.call和Function.prototype.apply仅使用一个参数,javascript,Javascript,Function.prototype.call或Function.prototype.apply只使用一个参数做什么 这是怎么回事 Function.prototype.myBind = function (context) { var fun = this; return function(){ return fun.call(context); } } 它调用函数时不传递任何参数。它调用函数时不传递任何参数 fun.call(context) fun中的函数将使用co

Function.prototype.call或Function.prototype.apply只使用一个参数做什么

这是怎么回事

Function.prototype.myBind = function (context) {
  var fun = this;
  return function(){
    return fun.call(context);
  }
}

它调用函数时不传递任何参数。

它调用函数时不传递任何参数

fun.call(context)
fun中的函数将使用context的context*调用,并且不传递任何参数

这本质上等同于调用:

context.temp = fun;
context.temp();
当然,调用时不会添加其他属性

下面是一个例子:

var a={foo:'bar'}, b={foo:'baz'}; 函数示例{ console.logthis.foo; } log'example called on a'; example.calla;//'酒吧 log'example called on b'; example.callb;//'巴兹 fun中的函数将使用context的context*调用,并且不传递任何参数

这本质上等同于调用:

context.temp = fun;
context.temp();
当然,调用时不会添加其他属性

下面是一个例子:

var a={foo:'bar'}, b={foo:'baz'}; 函数示例{ console.logthis.foo; } log'example called on a'; example.calla;//'酒吧 log'example called on b';
example.callb;//'baz'它与context.fun有何不同?@WylliamJudd:context.fun不存在。fun是一个变量,不是属性名。它与context.fun有何不同?@WylliamJudd:context.fun不存在。fun是一个变量,而不是属性名。