> Function.call == Function.prototype.call
true
> Function.prototype == Function
false
Run Code Online (Sandbox Code Playgroud)
为什么Function.prototype.*方法存在Function.*?这似乎不一致.
任何其他主要类型都不是这种情况(Array.slice不存在但是Array.prototype.slice确实如此).
the*_*eye 18
因为Function它本身就是原型Function
console.log(Function instanceof Function);
console.log(Object.getPrototypeOf(Function) === Function.prototype);
Run Code Online (Sandbox Code Playgroud)
因此,Functions原型中的所有功能也可用Function.
引用规范,
Function原型对象本身就是一个Function对象(它的[[Class]]是"Function")
确认这一点的另一种方法是,
console.log(Function.call === Function.prototype.call);
Run Code Online (Sandbox Code Playgroud)
它意味着Function.call对象和Function.prototype.call对象是相同的.
console.log(Function.hasOwnProperty('call'));
Run Code Online (Sandbox Code Playgroud)
它意味着Function对象本身没有call属性.
console.log(Function.prototype.hasOwnProperty('call'));
Run Code Online (Sandbox Code Playgroud)
它意味着Function.prototype对象具有call属性.
Array.slice不存在,但Array.prototype.slice做
因为Array函数的原型是Function对象,而不是Array对象.
console.log(Object.getPrototypeOf(Array) === Function.prototype);
Run Code Online (Sandbox Code Playgroud)
这就是为什么我们得到call,apply,bind等的Array功能.它的Array对象一直是原型Array,然后slice就可以在Array对象上使用了.
| 归档时间: |
|
| 查看次数: |
711 次 |
| 最近记录: |