Function, Object, Array 与instanceof 连用时的问题。

一. 不管是Function, Object, Array这些都是构造函数。

Function
ƒ Function() { [native code] }
Function.prototype
ƒ () { [native code] }
Function.__proto__
ƒ () { [native code] }

Object
ƒ Object() { [native code] }
Object.prototype
{constructor: ƒ, __defineGetter__: ƒ, __defineSetter__: ƒ, hasOwnProperty: ƒ, __lookupGetter__: ƒ, …}
Object.__proto__
ƒ () { [native code] }

console.log(Object.__proto__.__proto__)
VM884:1 {constructor: ƒ, __defineGetter__: ƒ, __defineSetter__: ƒ, hasOwnProperty: ƒ, __lookupGetter__: ƒ

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

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

Function.prototype.__proto__
{constructor: ƒ, __defineGetter__: ƒ, __defineSetter__: ƒ, hasOwnProperty: ƒ, __lookupGetter__: ƒ, …}


Object.__proto__.__proto__
{constructor: ƒ, __defineGetter__: ƒ, __defineSetter__: ƒ, hasOwnProperty: ƒ, __lookupGetter__: ƒ, …}constructor: ƒ Object()hasOwnProperty: ƒ hasOwnProperty()isPrototypeOf: ƒ isPrototypeOf()propertyIsEnumerable: ƒ propertyIsEnumerable()toLocaleString: ƒ toLocaleString()toString: ƒ toString()valueOf: ƒ valueOf()__defineGetter__: ƒ __defineGetter__()__defineSetter__: ƒ __defineSetter__()__lookupGetter__: ƒ __lookupGetter__()__lookupSetter__: ƒ __lookupSetter__()get __proto__: ƒ __proto__()set __proto__: ƒ __proto__()


Object.__proto__.__proto__.__proto__
null

使用instanceof 时 左边的对象里的__proto__对象包含右边这个函数原型的话就是true;

Object instanceof Function   这里function.prototype (函数原型) 的值为 f() { native code }    ,而Object.__proto__(对象原型)的值现在也为 f () { natvie code } ;

也就是说f () { native code } 这个方法对象是创建 Object,Function, Array, 包括 RegExp, String Boolean  这些原始对象之父。 用new生成的对象。

而且 f () { natvie code } .__proto__ 指向了 {  } 这个。 而Object.prototype (对象构造函数的原型) 直接指向了 {  } 这个。

所有会有 Function instanceof Object  // true    ------> 拆解。 Function.__proto__.__proto__ === Object.prototype  

猜你喜欢

转载自www.cnblogs.com/george-9/p/9223991.html