繁体   English   中英

仅带有一个参数的Function.prototype.call和Function.prototype.apply

[英]Function.prototype.call and Function.prototype.apply with only one argument

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();

当然,通过call不会添加任何其他属性。

这是一个例子:

 var a = {foo: 'bar'}, b = {foo: 'baz'}; function example() { console.log(this.foo); } console.log('example called on a'); example.call(a); //'bar' console.log('example called on b'); example.call(b); //'baz' 

* this在函数里面

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM