JavaScript中instanceof與typeof運(yùn)算符的用法及區(qū)別詳細(xì)解析
JavaScript中的instanceof和typeof常被用來判斷一個(gè)變量是什么類型的(實(shí)例),但它們的使用還是有區(qū)別的:
typeof 運(yùn)算符
返回一個(gè)用來表示表達(dá)式的數(shù)據(jù)類型的字符串。
typeof expression ;
expression 參數(shù)是需要查找類型信息的任意表達(dá)式。
說明
typeof 是一個(gè)一元運(yùn)算符,放在一個(gè)運(yùn)算數(shù)之前。
typeof 運(yùn)算符把類型信息當(dāng)作字符串返回。typeof 返回值有六種可能: “number” ,“string”, “boolean”, “object” ,“function”, 和 “undefined.”
(而 ECMAScript 有 5 種原始類型(primitive type),即 Undefined、Null、Boolean、Number 和 String。)
注釋:
1、我們上面提到了ECMAScript的5種原始類型,在使用typeof操作符時(shí),我們需要特別區(qū)分"對象類型"與"對象值"(字面值)的差別。例如Boolean 對象是 Boolean 原始類型的引用類型,而true和false則是Boolean對象的兩個(gè)可能的對象值。我們可以把 ECMAScript的預(yù)定義對象(相對于其他語言中的類)看作是 相應(yīng)類型的原始值的封裝(或包裝)。而ECMAScript的所有預(yù)定義對象又都是繼承于Object對象。因此存在如下情況:
var testvar= new Number(68);
alert(typeof testvar); //輸出 "object"
testvar= 68;
alert(typeof testvar); //輸出 "number"
又如:
function Person(){}
document.write ("<br>typeof(Person):"+typeof(Person)); //function
var person = new Person();
document.write ("<br>typeof(person):"+typeof(person)); //object
注意:從傳統(tǒng)意義上來說,ECMAScript 并不真正具有類。事實(shí)上,除了說明不存在類,在 ECMA-262 中根本沒有出現(xiàn)“類”這個(gè)詞。ECMAScript 定義了“對象定義”,邏輯上等價(jià)于其他程序設(shè)計(jì)語言中的類。
另外:這些預(yù)定義對象覆蓋了Object 對象的 ValueOf() 方法,返回其原始值。而這些對象的所有屬性和方法都可應(yīng)用于相應(yīng)類型的原始值上,因?yàn)樗鼈兪莻螌ο蟆?/P>
2、typeof 運(yùn)算符對于 null 值會返回 "Object"。這實(shí)際上是 JavaScript 最初實(shí)現(xiàn)中的一個(gè)錯(cuò)誤,然后被 ECMAScript 沿用了。現(xiàn)在,null 被認(rèn)為是對象的占位符,從而解釋了這一矛盾,但從技術(shù)上來說,它仍然是原始值。
提示:
1、值 undefined 并不同于未定義的值。但是,typeof 運(yùn)算符并不真正區(qū)分這兩種值。考慮下面的代碼:
var oTemp;
alert(typeof oTemp); //輸出 "undefined"
alert(typeof oTemp2); //輸出 "undefined"
前面的代碼對兩個(gè)變量輸出的都是 "undefined",即使只有變量 oTemp2 從未被聲明過。如果對 oTemp2 使用除 typeof 之外的其他運(yùn)算符的話,會引起錯(cuò)誤,因?yàn)槠渌\(yùn)算符只能用于已聲明的變量上。
2、當(dāng)函數(shù)無明確返回值時(shí),返回的也是值 "undefined",如下所示:
function testFunc() {}
alert(testFunc() == undefined); //輸出 "true"3、類型Null,它只有一個(gè)專用值 null,即它的字面量。值 undefined 實(shí)際上是從值 null 派生來的,因此 ECMAScript 把它們定義為相等的。
alert(null == undefined); //輸出 "true"
盡管這兩個(gè)值相等,但它們的含義不同:
undefined 是聲明了變量但未對其初始化時(shí)賦予該變量的值 或 未聲明過的變量(只能用于typeof,但作為賦值目標(biāo)時(shí)編譯器會自動(dòng)將其聲明為全局變量)。
null 則用于表示尚未存在的對象(即對象為空,或?qū)ο笳也坏剑H绻瘮?shù)或方法要返回的是對象,那么找不到該對象時(shí),返回的通常是 null。
3、我們可以使用 typeof 來獲取一個(gè)變量是否存在,如 if(typeof a!="undefined"){alert("ok")},而不要去使用 if(a) 因?yàn)槿绻?a 不存在(未聲明)則會出錯(cuò)。
對于 Array,Null 等特殊對象使用 typeof 一律返回 object,這正是 typeof 的局限性。如果我們希望獲取一個(gè)對象是否是數(shù)組,或判斷某個(gè)變量是否是某個(gè)對象的實(shí)例則要選擇使用instanceof。
instanceof 運(yùn)算符
在使用 typeof 運(yùn)算符時(shí)采用引用類型存儲值會出現(xiàn)一個(gè)問題,無論引用的是什么類型的對象,它都返回 "object"。ECMAScript 引入了另一個(gè) Java 運(yùn)算符 instanceof 來解決這個(gè)問題。
instanceof 運(yùn)算符與 typeof 運(yùn)算符相似,用于識別正在處理的對象的類型。與 typeof 方法不同的是,instanceof 方法要求開發(fā)者明確地確認(rèn)對象為某特定類型。例如:
var oStringObject = new String("hello world");
alert(oStringObject instanceof String); //輸出 "true"
這段代碼問的是“變量 oStringObject 是否為 String 對象的實(shí)例?”oStringObject 的確是 String 對象的實(shí)例,因此結(jié)果是 "true"。盡管不像 typeof 方法那樣靈活,但是在 typeof 方法返回 "object" 的情況下,instanceof 方法還是很有用的。
instanceof運(yùn)算符
是一個(gè)二元運(yùn)算符。返回一個(gè) Boolean 值,指出對象是否是特定類的一個(gè)實(shí)例。
expression instanceof class
參數(shù)
expression 必選項(xiàng)。任意對象表達(dá)式。
class 必選項(xiàng)。任意已定義的對象類。
說明
如果 object 是 class 的一個(gè)實(shí)例,則 instanceof 運(yùn)算符返回 true 。如果 object不是指定類的一個(gè)實(shí)例,或者 object 是 null ,則返回 false 。
用于判斷一個(gè)變量是否某個(gè)對象的實(shí)例,
如var a=new Array();alert(a instanceof Array);會返回true,同時(shí)alert(a instanceof Object)也會返回true;這是因?yàn)锳rray是object的子類。
再如:function test(){};var a=new test();alert(a instanceof test)會返回true。
注意:
關(guān)于function 的 arguments,我們大家也許都認(rèn)為 arguments 是一個(gè) Array,但如果使用 instaceof 去測試會發(fā)現(xiàn) arguments 不是一個(gè) Array 對象,盡管看起來很像。
此外還有類似的情況,例如:
var a=new Array();if (a instanceof Object) alert('Y');else alert('N'); 得'Y'
但 if (window instanceof Object) alert('Y');else alert('N'); 得'N'
所以,這里的 instanceof 測試的 object 是指 js 語法中的 object,不是指 dom 模型對象。
而此時(shí)使用 typeof 會有些區(qū)別: alert(typeof(window)) 會得 object
引申:JavaScript中的instanceof操作符的原理是什么?
學(xué)習(xí)js時(shí),了解到在判斷js中一個(gè)實(shí)例是否屬于某一種類型時(shí),可以使用instanceof操作符,比如function Person(){}
var person = new Person(); alert(person instanceof Person);//返回true
那么在執(zhí)行instanceof這個(gè)操作時(shí)經(jīng)過了怎樣的判斷,返回了true/false?
會不會是通過判斷Person.prototype與person的內(nèi)部指針[[prototype]]兩者引用是否相同而得出結(jié)果的?
其實(shí),凡是能在實(shí)例的"原型對象鏈"中找到該構(gòu)造函數(shù)的prototype屬性所指向的原型對象,就返回true。
而prototype根本就不是實(shí)例具有的屬性(或者說實(shí)例的prototype屬性為undefined),而是它原型對象中的屬性,如果被篡改了,這個(gè)判斷方法就不能正確返回了。
另外,能不能直接判斷 person.constructor == Person來取得想要的結(jié)果呢?
我們做個(gè)測試,如下JavaScript代碼:
function Person(name,sex){this.name=name;this.sex=sex;}
document.write ("<br>typeof Person:"+typeof Person);
document.write ("<br>typeof Person.prototype:"+typeof Person.prototype);
document.write ("<br>typeof Person.constructor:"+typeof Person.constructor);
var person = new Person();
document.write ("<br><br>var person = new Person();");
document.write ("<br>typeof person:"+typeof person);
document.write ("<br>typeof person.prototype:"+typeof person.prototype);
document.write ("<br>typeof person.constructor:"+typeof person.constructor);
document.write ("<br><br>Function.constructor:"+Function.constructor);
document.write ("<br><br>Function.prototype:"+Function.prototype);
document.write ("<br><br>Person.constructor:"+Person.constructor);
document.write ("<br><br>Person.prototype:"+Person.prototype);
document.write ("<br><br>person.constructor:"+person.constructor);
document.write ("<br><br>person.prototype:"+person.prototype);
輸出如下:
typeof Person:function
typeof Person.prototype:object
typeof Person.constructor:function
var person = new Person();
typeof person:object
typeof person.prototype:undefined
typeof person.constructor:function
Function.constructor:function Function() { [native code] }
Function.prototype:function Empty() {}
Person.constructor:function Function() { [native code] }
Person.prototype:[object Object]
person.constructor:function Person(name,sex){this.name=name;this.sex=sex;}
person.prototype:undefined
和Function類似,Number()為Number對象的構(gòu)造函數(shù),Number()用于將其參數(shù)轉(zhuǎn)換為數(shù)字number類型,并返回轉(zhuǎn)換結(jié)果(若不能轉(zhuǎn)換則返回NaN)。
在JavaScript中constructor較少使用,variable.constructor返回其對象類的構(gòu)造函數(shù)的字符串表示。
那么在JavaScript中判斷數(shù)據(jù)類型時(shí),我們可以使用以下方式來得到其詳細(xì)數(shù)據(jù)類型:
if((typeof a=="object") && (a.constructor==Array)){
…
}
注意:constructor只能對已有變量進(jìn)行判斷,而typeof則可對未聲明變量或空對象進(jìn)行判斷(返回undefined)。
相關(guān)文章
JavaScript實(shí)現(xiàn)的簡單加密解密操作示例
這篇文章主要介紹了JavaScript實(shí)現(xiàn)的簡單加密解密操作,涉及javascript基于charCodeAt與fromCharCode的字符串編碼與解碼操作相關(guān)使用技巧,需要的朋友可以參考下2018-06-06微信小程序?qū)崿F(xiàn)頁面浮動(dòng)導(dǎo)航
這篇文章主要為大家詳細(xì)介紹了微信小程序?qū)崿F(xiàn)頁面浮動(dòng)導(dǎo)航,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01微信小程序?qū)崿F(xiàn)授權(quán)登錄之獲取用戶信息
這篇文章主要介紹了微信小程序?qū)崿F(xiàn)授權(quán)登錄之獲取用戶信息,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05JavaScript學(xué)習(xí)小結(jié)之使用canvas畫“哆啦A夢”時(shí)鐘
這篇文章主要介紹了JavaScript學(xué)習(xí)小結(jié)之使用canvas畫“哆啦A夢”時(shí)鐘的相關(guān)資料,需要的朋友可以參考下2016-07-07