Object.call() 和 Object.__proto__.call() 有什么区别?

What's the difference Object.call() and Object.__proto__.call()?

对象没有自己的方法调用,所以它从 proto 中获取它,但为什么结果不同?

// Look at results in your browser's console

console.log(
  Object.call(null,2), // Number {2}
  Object.__proto__.call(null,2), // undefined

  Object.call(null,''), // String {""}
  Object.__proto__.call(null,'') // undefined
);

Object.call 是对 Function.prototype.call 的引用(因为 Object.__proto__Function.prototype):

console.log(Object.call === Function.prototype.call);
console.log(Object.__proto__.call === Function.prototype.call);
console.log(Object.__proto__ === Function.prototype);

Object 是一个构造函数(例如,new Object(...) 给你一个对象)。调用 Object.call 会调用 Function.prototype.call,调用上下文为 ObjectFunction.prototype.call 用于确定需要调用哪个函数。

所以

Object.call(null,2)

基本相同
Object(2);

它给你一个 number 对象包装器。

相比之下,使用 Object.__proto__.call,您将使用 Object.__proto__ 的调用上下文调用 Function.prototype.call。但是 Object.__proto__Function.prototypeFunction.prototype, per the specification:

accepts any arguments and returns undefined when invoked.

console.log(Function.prototype());

所以,当用它调用 .call 时,无论如何,它只会 return undefined (但它不会抛出错误或任何东西,即使你可能认为 应该 - 调用 Function.prototype 没有任何意义)。

以下表达式都在做完全相同的事情:

Object.__proto__.call(null,2) // undefined
Function.prototype.call(null,2) // undefined
Function.prototype(2) // undefined