call
我们先来看个例子
function Product(name, price) {
  this.name = name;
  this.price = price;
}
function Food(name, price) {
  Product.call(this, name, price);
  this.category = 'food';
}
console.log(new Food('cheese', 5).name);
// expected output: "cheese"Function.prototype.call = function(context) {
  context = context ? Object(context) : window;
  context.fn = this;
  let args = [];
  for(let i = 1; i < arguments.length; i++){
    args.push(arguments[i])
  }
  let res = eval(`${context.fn(args)}`)
  delete context.fn;
  return res
}
最后更新于
这有帮助吗?