解析JavaScript中instanceof對于不同的構(gòu)造器或許都返回true
我們知道 instanceof 運(yùn)算符用來檢查對象是否為某構(gòu)造器的實(shí)例。下面列舉它返回true的各種情景。
1、對象obj是通過new Constructor創(chuàng)建的,那么 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、如果存在繼承關(guān)系,那么 子類實(shí)例 instanceof 父類 也會(huì)返回true
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
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)造器自身
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
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
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
// 父類
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)就不奇怪了
// 定義兩個(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)部原型鏈中,鏈條被打斷了。
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
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異常,
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)于javascript中的typeof和instanceof介紹
- JavaScript中instanceof運(yùn)算符的用法總結(jié)
- javascript之typeof、instanceof操作符使用探討
- JavaScript instanceof 的使用方法示例介紹
- 談?wù)勎覍avaScript中typeof和instanceof的深入理解
- JavaScript類型檢測之typeof 和 instanceof 的缺陷與優(yōu)化
- 淺談javascript中的instanceof和typeof
- JavaScript必知必會(huì)(六) delete in instanceof
- JavaScript中instanceof運(yùn)算符的使用示例
- 實(shí)例講解JavaScript中instanceof運(yùn)算符的用法
- JavaScript的instanceof運(yùn)算符學(xué)習(xí)教程
相關(guān)文章
網(wǎng)頁運(yùn)行時(shí)提示對象不支持abigimage屬性或方法
網(wǎng)頁中用了一個(gè)js插件,js文件引用的沒有錯(cuò)但是運(yùn)行時(shí)ie的調(diào)試工具報(bào)了一個(gè)錯(cuò),提示對象不支持abigimage屬性或方法2014-08-08document.all與getElementById、getElementsByName、getElementsByT
HTML DOM 定義了多種查找元素的方法,除了 getElementById() 之外,還有 getElementsByName() 和 getElementsByTagName()。2008-12-12JS實(shí)現(xiàn)網(wǎng)頁背景顏色與select框中顏色同時(shí)變化的方法
這篇文章主要介紹了JS實(shí)現(xiàn)網(wǎng)頁背景顏色與select框中顏色同時(shí)變化的方法,實(shí)例分析了javascript操作select及css樣式的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-02-02javascript實(shí)現(xiàn)tabs選項(xiàng)卡切換效果(自寫原生js)
常用的頁面效果有彈出層效果,無縫滾動(dòng)效果,選項(xiàng)卡切換效果,接下來與大家分享一款自己用原生javascript寫的選項(xiàng)卡切換效果,感興趣的朋友可以參考下哈2013-03-03js實(shí)現(xiàn)通過開始結(jié)束控制的計(jì)時(shí)器
這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)通過開始結(jié)束控制的計(jì)時(shí)器,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-02-02Bootstrap基本插件學(xué)習(xí)筆記之標(biāo)簽切換(17)
這篇文章主要為大家詳細(xì)介紹了Bootstrap基本插件學(xué)習(xí)筆記之標(biāo)簽切換的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12