我們知道 instanceof 運算符用來檢查對象是否為某構造器的實例。下面列舉它返回true的各種情景。
1、對象obj是通過new Constructor創建的,那么 obj instanceof Constructor 為true
function Person(n, a) {
this.name = n;
this.age = a;
}
var p = new Person('John Backus', 82);
console.log(p instanceof Person); // true
2、如果存在繼承關系,那么 子類實例 instanceof 父類 也會返回true
function A(){}
function B(){}
B.prototype = new A(); // B繼承于A
var b = new B();
console.log(b instanceof A); // true
3、由于Object是根類,所有其它自定義類都繼承于它,因此 任意構造器的實例 instanceof Object 都返回true
function A() {}
var a = new A();
console.log(a instanceof Object); // true
var str = new String('hello');
console.log(str instanceof Object); // true
var num = new Number(1);
console.log(num instanceof Object); // true
甚至包括構造器自身
function A() {}
console.log(A instanceof Object); // true
console.log(String instanceof Object); // true
console.log(Number instanceof Object); // true
4、所有構造器 instanceof Function 返回true
function A() {}
console.log(A instanceof Function); // true
console.log(String instanceof Function); // true
console.log(Number instanceof Function); // true
以上四點總結為一句話:如果某實例是通過某類或其子類的創建的,那么instanceof就返回true。或者說某構造函數的原型 存在與對象obj的內部原型鏈上,那么返回true。即instanceof的結果與構造器自身并無直接關系。這在許多語言中都是通用的。Java中定義了一個類Person,實例p對于Person和Object都返回true
class Person {
public String name;
public int age;
Person (String n, int a) {
this.name = name;
this.age = a;
}
public static void main(String[] args) {
Person p = new Person("John Backus", 82);
System.out.println(p instanceof Person); // true
System.out.println(p instanceof Object); // true
}
}
Java中如果存在繼承關系,那么 子類實例 instanceof 父類 也返回true
// 父類
class Person {
public String name;
public int age;
Person (String n, int a) {
name = name;
age = a;
}
}
// 子類
public class Man extends Person{
public String university;
Man(String n, int a, String s) {
super(n, a);
university = s;
}
public static void main(String[] args) {
Man mm = new Man("John Resig", 29, "PKU");
System.out.println(mm instanceof Man); // true
System.out.println(mm instanceof Person); // 也是true
}
}
知道了這些,JS中以下的表現就不奇怪了
// 定義兩個構造器
function A(){}
function B(){}
A.prototype = B.prototype = {a: 1};
// 分別創建兩個不同構造器的實例
var a = new A();
var b = new B();
console.log(a instanceof B); // true
console.log(b instanceof A); // true
我們看到a, b分別是用A和B創建的,但a instanceof B和 b instanceof A都是true。即a雖然不是用構造器B創建的,但仍然返回true。因為B.prototype存在于a的內部原型鏈上。由于JS的動態語言特性,可以在運行時修改原型,因此下面返回false也不足為奇了。因為A.prototype已經不在a的內部原型鏈中,鏈條被打斷了。
function A(){}
var a = new A();
A.prototype = {}; // 動態修改原型,注意必須在創建a后
console.log(a instanceof A); // false
注意這么寫也打破了上面總結的第一條:對象obj是通過new Constructor創建的,那么obj instanceof Constructor 為true實際在ECMAScript標準中(以5.1為準),instanceof 內部實現會調用構造器的內部方法[[HasInstance]],描述如下

假如F是一個函數對象,當F(V)執行時,以下步驟將發生:
1、如果instanceof左運算元V不是對象類型,直接返回false
var a, b = 1, c = true, d = 'hello';
console.log(a instanceof Object); // false 這里a值為undefined
console.log(b instanceof Object); // false
console.log(c instanceof Object); // false
console.log(d instanceof Object); // false
2/3、取構造器F的prototype屬性,如果不是對象類型,須拋出TypeError異常,
function A(){}
A.prototype = 1; // A的prototype設為非對象類型
var a = new A();
console.log(a instanceof A);
各瀏覽器拋出的異常提示不同,Firefox18:

Chrome24:

Safari6:

Opera12:

IE10:
4、不斷的執行以下邏輯:將V設為內部原型的V,如果V是null則返回false,如果V和O都指向同一個對象,則返回true。