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  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM