Uncaught TypeError: Cannot convert undefined or null to object at push (<anonymous>)

Uncaught TypeError: Cannot convert undefined or null to object at push (<anonymous>)

为什么这会引发错误,

const callback = Array.prototype.push;
callback(1)

但这不会,

Array.prototype.push(1)

我知道原因是第一种情况下 this 上下文丢失了,但是你能给我更多细节吗?

this,在函数内部,取决于(在大多数情况下)调用函数的方式。 callback() 在没有任何对象的情况下被调用。在这种情况下,这将指向全局对象 window(如果您在浏览器中)。你不能做 window.push().

额外:箭头函数是 most cases 语句的一个例外。