莫名的奇妙,Call()

67 阅读1分钟

call是什么意思?

The call() method calls a function with a given this value and arguments provided individually.

The call() method calls a function with a given this value and arguments provided individually.

以下是官方示例:

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

function Food(name, price) {
  Product.call(this, name, price);  
  // call方法其参数`this`是指Food?
  this.category = 'food';
}

console.log(new Food('cheese', 5).name);
// expected output: "cheese"

与寻常函数不同,Product 里面有 this. ,而且函数名还大写了首字母。

是Food call Product,还是 被Product call呢?

是 Product 在 Food 里面 call 了 Food 的数据, 还是 Food 调用了(call了)Product 的功能?