欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

解析JavaScript中instanceof對于不同的構(gòu)造器或許都返回true

 更新時(shí)間:2013年12月03日 09:37:51   作者:  
這篇文章主要是對JavaScript中instanceof對于不同的構(gòu)造器或許都返回true進(jìn)行了詳細(xì)的解析,需要的朋友可以過來參考下,希望對大家有所幫助

我們知道 instanceof 運(yùn)算符用來檢查對象是否為某構(gòu)造器的實(shí)例。下面列舉它返回true的各種情景。

1、對象obj是通過new Constructor創(chuàng)建的,那么 obj instanceof Constructor 為true

復(fù)制代碼 代碼如下:

function Person(n, a) {
    this.name = n;
    this.age = a;
}
var p = new Person('John Backus', 82);
console.log(p instanceof Person); // true

2、如果存在繼承關(guān)系,那么 子類實(shí)例 instanceof 父類 也會(huì)返回true
復(fù)制代碼 代碼如下:

function A(){}
function B(){}
B.prototype = new A(); // B繼承于A

var b = new B();
console.log(b instanceof A); // true

3、由于Object是根類,所有其它自定義類都繼承于它,因此 任意構(gòu)造器的實(shí)例 instanceof Object 都返回true
復(fù)制代碼 代碼如下:

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

甚至包括構(gòu)造器自身
復(fù)制代碼 代碼如下:

function A() {}
console.log(A instanceof Object); // true
console.log(String instanceof Object); // true
console.log(Number instanceof Object); // true

4、所有構(gòu)造器 instanceof Function 返回true
復(fù)制代碼 代碼如下:

function A() {}
console.log(A instanceof Function); // true
console.log(String instanceof Function); // true
console.log(Number instanceof Function); // true

以上四點(diǎn)總結(jié)為一句話:如果某實(shí)例是通過某類或其子類的創(chuàng)建的,那么instanceof就返回true。或者說某構(gòu)造函數(shù)的原型 存在與對象obj的內(nèi)部原型鏈上,那么返回true。即instanceof的結(jié)果與構(gòu)造器自身并無直接關(guān)系。這在許多語言中都是通用的。

Java中定義了一個(gè)類Person,實(shí)例p對于Person和Object都返回true

復(fù)制代碼 代碼如下:

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中如果存在繼承關(guān)系,那么 子類實(shí)例 instanceof 父類 也返回true
復(fù)制代碼 代碼如下:

// 父類
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中以下的表現(xiàn)就不奇怪了
復(fù)制代碼 代碼如下:

// 定義兩個(gè)構(gòu)造器
function A(){}
function B(){}
A.prototype = B.prototype = {a: 1};

// 分別創(chuàng)建兩個(gè)不同構(gòu)造器的實(shí)例
var a = new A();
var b = new B();
console.log(a instanceof B); // true
console.log(b instanceof A); // true

我們看到a, b分別是用A和B創(chuàng)建的,但a instanceof B和 b instanceof A都是true。即a雖然不是用構(gòu)造器B創(chuàng)建的,但仍然返回true。因?yàn)锽.prototype存在于a的內(nèi)部原型鏈上。

由于JS的動(dòng)態(tài)語言特性,可以在運(yùn)行時(shí)修改原型,因此下面返回false也不足為奇了。因?yàn)锳.prototype已經(jīng)不在a的內(nèi)部原型鏈中,鏈條被打斷了。

復(fù)制代碼 代碼如下:

function A(){}
var a = new A();
A.prototype = {}; // 動(dòng)態(tài)修改原型,注意必須在創(chuàng)建a后
console.log(a instanceof A); // false

注意這么寫也打破了上面總結(jié)的第一條:對象obj是通過new Constructor創(chuàng)建的,那么obj instanceof Constructor 為true

實(shí)際在ECMAScript標(biāo)準(zhǔn)中(以5.1為準(zhǔn)),instanceof 內(nèi)部實(shí)現(xiàn)會(huì)調(diào)用構(gòu)造器的內(nèi)部方法[[HasInstance]],描述如下

假如F是一個(gè)函數(shù)對象,當(dāng)F(V)執(zhí)行時(shí),以下步驟將發(fā)生:

1、如果instanceof左運(yùn)算元V不是對象類型,直接返回false

復(fù)制代碼 代碼如下:

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、取構(gòu)造器F的prototype屬性,如果不是對象類型,須拋出TypeError異常,
復(fù)制代碼 代碼如下:

function A(){}
A.prototype = 1; // A的prototype設(shè)為非對象類型
var a = new A();
console.log(a instanceof A);

各瀏覽器拋出的異常提示不同,

Firefox18:

Chrome24:

Safari6:

Opera12:

IE10:

  

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

相關(guān)文章

最新評論