Array-Object-Function

2020-07-05  本文已影响0人  梦行乌托邦
img1
img2
img3

以上三图发现,Array、Object、Function之间有一些共同的方法

// instanceof 运算符用于检测构造函数的 prototype 属性是否出现在某个实例对象的原型链上。
Function instanceof Object  // true
Array instanceof Object  // true

Function.prototype.__proto__ == Object.prototype // true
Array.prototype.__proto__ == Object.prototype // true

Object.prototype.isPrototypeOf(Function) // true
Object.prototype.isPrototypeOf(Array) // true

Function和Array的prototype都是Object的实例?

JS中所有的对象都是Object的实例,并继承Object.prototype的属性和方法。
因为Array.prototype、Function.prototype是对象,所以它们是Object的实例。

isPrototypeOf、hasOwnProperty、propertyIsEnumerable、valueOf这些都是Object.prototype自己的方法,那么Array和Object能用这些方法,说明Object.prototype在它们的原型链上。

Object.prototype.hasOwnProperty('valueOf') // true
Array.prototype.hasOwnProperty('valueOf') // true
Function.prototype.hasOwnProperty('valueOf') // true

它们都实现了valueOf方法

Array.hasOwnProperty('bind') // false
Array.prototype.hasOwnProperty('bind') // false
Object.hasOwnProperty('bind') // false
Object.prototype.hasOwnProperty('bind') // false
Function.hasOwnProperty('bind') // false
Function.prototype.hasOwnProperty('bind') // true

Array instanceof Function // true
Object instanceof Function // true

Function.prototype.isPrototypeOf(Object) // true
Function.prototype.isPrototypeOf(Array) // true

bind、apply、call、caller、arguments这些都只是Function.prototype自己的方法,那么Array和Object能用这些方法,说明Function.prototype在Array和Object的原型链上?以上代码就证明了这一点。但是
Object.prototype在Array和Function的原型链上
Function.prototype在Array和Object的原型链上
Object和Function的关系???

上一篇下一篇

猜你喜欢

热点阅读