> Function.call == Function.prototype.call
true
> Function.prototype == Function
false为什么Function.prototype.*方法以Function.*的形式存在?这似乎不一致。
任何其他主要类型都不是这样(Array.slice不存在,但Array.prototype.slice存在)。
发布于 2016-01-15 09:09:25
因为Function本身就是Function的原型
console.log(Function instanceof Function);
console.log(Object.getPrototypeOf(Function) === Function.prototype);因此,Function原型中的所有功能也可以在Function中使用。
引用the specification的话,
函数原型对象本身就是一个函数对象(它的[Class]是“函数”)
另一种确认这个的方法是,
console.log(Function.call === Function.prototype.call);这意味着Function.call对象和Function.prototype.call对象是相同的。
console.log(Function.hasOwnProperty('call'));这意味着Function对象本身不具有call属性。
console.log(Function.prototype.hasOwnProperty('call'));这意味着Function.prototype对象具有call属性。
Array.slice不存在,但Array.prototype.slice存在
因为Array函数的原型是Function对象,而不是Array对象。
console.log(Object.getPrototypeOf(Array) === Function.prototype);这就是为什么我们得到call,apply,bind等的Array函数。它的Array对象是Array的原型,那么slice也可以在Array对象上使用。
https://stackoverflow.com/questions/34807583
复制相似问题