Function, Object, Array 與instanceof 連用時的問題。

一. 不論是Function, Object, Array這些都是構造函數。函數

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

 

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

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

 

Function.prototype.__proto__ === Object.prototype  // trueget

 

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

 

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


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__()console


Object.__proto__.__proto__.__proto__
nullfunction

使用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  

相關文章
相關標籤/搜索