DIV CSS 佈局教程網

instanceof 運算符的判定機制與原型鏈細節
編輯:JavaScript基礎知識     

語法

object instanceof constructor

左側是要檢測的物件,右側是某個建構函式。

它到底在判斷什麼

instanceof 檢查的是:右側建構函式的 prototype 物件,是否出現在左側物件的原型鏈上。這句話解釋了絕大多數看似矛盾的結果。

function C() {}
function D() {}
var o = new C();

o instanceof C; // true,因為 Object.getPrototypeOf(o) === C.prototype
o instanceof D; // false,D.prototype 不在 o 的原型鏈上
o instanceof Object; // true,Object.prototype.isPrototypeOf(o) 為 true

C.prototype instanceof Object; // true,同上

C.prototype = {};
var o2 = new C();
o2 instanceof C; // true
o instanceof C;  // false,C.prototype 已換成新的空物件,不在 o 的原型鏈上

D.prototype = new C(); // 原型繼承
var o3 = new D();
o3 instanceof D; // true
o3 instanceof C; // true

結果並非一成不變

如果 obj instanceof Foo 為 true,不代表它永遠為 true。Foo.prototype 的值一旦被替換,新的 prototype 很可能不在 obj 的原型鏈上,結果就翻成 false。另一種情況是物件自身的原型鏈被改動——雖然 ES 規範只允許讀取原型,但非標準的 __proto__ 屬性讓這件事在實務上仍然做得到,例如執行 obj.__proto__ = {} 之後,判斷結果同樣會變成 false。

跨執行環境時會失效

瀏覽器中腳本常需要在多個視窗或 iframe 之間互動。每個視窗都是獨立的全域環境,各自擁有自己的內建建構函式。因此 [] instanceof window.frames[0].Array 會回傳 false,因為兩邊的 Array.prototype 根本不是同一個物件。要跨 frame 判斷陣列,請改用 Array.isArray(myObj) 或 Object.prototype.toString.call(myObj) === "[object Array]"。

基本用法

console.log(true instanceof Boolean);          // false
console.log(new Number(1) instanceof Number);  // true

第一個例子關鍵在於 true 是原始值而非物件,原型鏈上沒有 Boolean.prototype。

判斷父型別

function Father() {}
function Child() {}
Child.prototype = new Father();

var a = new Child();
console.log(a instanceof Child);  // true
console.log(a instanceof Father); // true

Child 的實例為何也算 Father 的實例?把 instanceof 的邏輯攤開寫就一目了然:

function check(a, b) {
  while (a.__proto__) {
    if (a.__proto__ === b.prototype) return true;
    a = a.__proto__;
  }
  return false;
}

console.log(Object instanceof Object === check(Object, Object));         // true
console.log(Function instanceof Function === check(Function, Function)); // true
console.log(Number instanceof Number === check(Number, Number));         // true
console.log(String instanceof String === check(String, String));         // true
console.log(Function instanceof Object === check(Function, Object));     // true
console.log(Foo instanceof Function === check(Foo, Function));           // true
console.log(Foo instanceof Foo === check(Foo, Foo));                     // true

換句話說,a 若是 b 的實例,a 的原型鏈上必定存在與 b.prototype 相同的物件,於是一層層往上找即可。

為什麼函式本身就是 Function 的實例

String、Number、Boolean、Function 這些都是函式,而函式一律由 Function 建構而來,因此它們同樣能讀到 Function.prototype 上的成員:

Function.prototype.a = 10;
console.log(String.a); // 10

逐步推演兩個經典判斷式

// 左式與右式先分開標記,方便逐步替換
FunctionL = Function, FunctionR = Function;

O = FunctionR.prototype = Function.prototype
L = FunctionL.__proto__ = Function.prototype
// 第一次判斷 O == L → 回傳 true
StringL = String, StringR = String;

O = StringR.prototype = String.prototype
L = StringL.__proto__ = Function.prototype
// 第一次判斷 O != L,繼續往上

L = String.prototype.__proto__ = Object.prototype
// 第二次判斷 O != L,繼續往上

L = String.prototype.__proto__ = null
// 第三次判斷 L == null → 回傳 false
XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved