Function.prototype.call和Function.protoype.all只应用一个参数

Function.prototype.call and Function.prototype.apply with only one argument

本文关键字:Function 参数 一个 call prototype protoype all 应用      更新时间:2023-09-26

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

这是怎么回事?

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

它在不传递任何参数的情况下调用函数。

fun.call(context)

CCD_ 1中的函数将使用CCD_。

这基本上相当于调用:

context.temp = fun;
context.temp();

当然对于CCD_ 3,将不添加额外的属性。

这里有一个例子:

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在函数内