Call

允许为不同的对象分配和调用属于一个对象的函数/方法。

call() 方法调用一个函数, 其具有一个指定的this值和分别地提供的参数(参数的列表)。

call接受的是若干个参数的列表,而apply接受的是一个包含多个参数的数组。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
//使用

function Produce(name,price){
    this.name = name;
    this.price = price;
}

function Food(name,price){
    Produce.call(this,name,price);
    this.category = "food";
}

console.log(new Food("cheese",5).name);
// cheese

Apply

1
func.apply(thisArg, [argsArray])

效率君

call的效率高于apply,因为apply的参数是数组,每次都需要循环。 call的参数则为函数直接需要的参数,个人理解apply是call的更高一级的封装。