JavaScript中instanceof與typeof運算符的用法及區別詳細解析

這篇文章主要是對JavaScript中instanceof與typeof運算符的用法及區別進行了詳細的分析介紹。需要的朋友可以過來參考下,希望對大家有所幫助

JavaScript中的instanceof和typeof常被用來判斷一個變量是什麼類型的(實例),但它們的使用還是有區別的:

typeof 運算符
返回一個用來表示表達式的數據類型的字符串。

typeof expression ;

expression 參數是需要查找類型信息的任意表達式。

說明
typeof 是一個一元運算符,放在一個運算數之前。

typeof 運算符把類型信息當作字符串返回。typeof 返回值有六種可能: “number” ,“string”, “boolean”, “object” ,“function”, 和 “undefined.”

(而 ECMAScript 有 5 種原始類型(primitive type),即 Undefined、Null、Boolean、Number 和 String。)

註釋:

1、我們上面提到了ECMAScript的5種原始類型,在使用typeof操作符時,我們需要特別區分"對象類型"與"對象值"(字面值)的差別。例如Boolean 對象是 Boolean 原始類型的引用類型,而true和false則是Boolean對象的兩個可能的對象值。我們可以把 ECMAScript的預定義對象(相對於其他語言中的類)看作是 相應類型的原始值的封裝(或包裝)。而ECMAScript的所有預定義對象又都是繼承於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

注意:從傳統意義上來說,ECMAScript 並不真正具有類。事實上,除了說明不存在類,在 ECMA-262 中根本沒有出現“類”這個詞。ECMAScript 定義了“對象定義”,邏輯上等價於其他程序設計語言中的類。

另外:這些預定義對象覆蓋了Object 對象的 ValueOf() 方法,返回其原始值。而這些對象的所有屬性和方法都可應用於相應類型的原始值上,因爲它們是僞對象。

2、typeof 運算符對於 null 值會返回 "Object"。這實際上是 JavaScript 最初實現中的一個錯誤,然後被 ECMAScript 沿用了。現在,null 被認爲是對象的佔位符,從而解釋了這一矛盾,但從技術上來說,它仍然是原始值。

提示:

1、值 undefined 並不同於未定義的值。但是,typeof 運算符並不真正區分這兩種值。考慮下面的代碼:

var oTemp;
alert(typeof oTemp);  //輸出 "undefined"
alert(typeof oTemp2);  //輸出 "undefined"

前面的代碼對兩個變量輸出的都是 "undefined",即使只有變量 oTemp2 從未被聲明過。如果對 oTemp2 使用除 typeof 之外的其他運算符的話,會引起錯誤,因爲其他運算符只能用於已聲明的變量上。

2、當函數無明確返回值時,返回的也是值 "undefined",如下所示:

function testFunc() {}
alert(testFunc() == undefined);  //輸出 "true"3、類型Null,它只有一個專用值 null,即它的字面量。值 undefined 實際上是從值 null 派生來的,因此 ECMAScript 把它們定義爲相等的。

alert(null == undefined);  //輸出 "true"
儘管這兩個值相等,但它們的含義不同:

undefined 是聲明瞭變量但未對其初始化時賦予該變量的值 或 未聲明過的變量(只能用於typeof,但作爲賦值目標時編譯器會自動將其聲明爲全局變量)。

null 則用於表示尚未存在的對象(即對象爲空,或對象找不到)。如果函數或方法要返回的是對象,那麼找不到該對象時,返回的通常是 null。

3、我們可以使用 typeof 來獲取一個變量是否存在,如 if(typeof a!="undefined"){alert("ok")},而不要去使用 if(a) 因爲如果 a 不存在(未聲明)則會出錯。

對於 Array,Null 等特殊對象使用 typeof 一律返回 object,這正是 typeof 的侷限性。如果我們希望獲取一個對象是否是數組,或判斷某個變量是否是某個對象的實例則要選擇使用instanceof。


instanceof 運算符
在使用 typeof 運算符時採用引用類型存儲值會出現一個問題,無論引用的是什麼類型的對象,它都返回 "object"。ECMAScript 引入了另一個 Java 運算符 instanceof 來解決這個問題。

instanceof 運算符與 typeof 運算符相似,用於識別正在處理的對象的類型。與 typeof 方法不同的是,instanceof 方法要求開發者明確地確認對象爲某特定類型。例如:

var oStringObject = new String("hello world");
alert(oStringObject instanceof String); //輸出 "true"
這段代碼問的是“變量 oStringObject 是否爲 String 對象的實例?”oStringObject 的確是 String 對象的實例,因此結果是 "true"。儘管不像 typeof 方法那樣靈活,但是在 typeof 方法返回 "object" 的情況下,instanceof 方法還是很有用的。

instanceof運算符

是一個二元運算符。返回一個 Boolean 值,指出對象是否是特定類的一個實例。

expression  instanceof class

參數

 expression  必選項。任意對象表達式。

 class  必選項。任意已定義的對象類。

說明
如果 object 是 class 的一個實例,則 instanceof 運算符返回 true 。如果 object不是指定類的一個實例,或者 object 是 null ,則返回 false 。

用於判斷一個變量是否某個對象的實例,

如var a=new Array();alert(a instanceof Array);會返回true,同時alert(a instanceof Object)也會返回true;這是因爲Array是object的子類。

再如:function test(){};var a=new test();alert(a instanceof test)會返回true。

注意:

關於function 的 arguments,我們大家也許都認爲 arguments 是一個 Array,但如果使用 instaceof 去測試會發現 arguments 不是一個 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 模型對象。

而此時使用 typeof 會有些區別: alert(typeof(window)) 會得 object

引申:JavaScript中的instanceof操作符的原理是什麼?

學習js時,瞭解到在判斷js中一個實例是否屬於某一種類型時,可以使用instanceof操作符,比如function Person(){}

var person = new Person();  alert(person instanceof Person);//返回true

那麼在執行instanceof這個操作時經過了怎樣的判斷,返回了true/false?

會不會是通過判斷Person.prototype與person的內部指針[[prototype]]兩者引用是否相同而得出結果的?

其實,凡是能在實例的"原型對象鏈"中找到該構造函數的prototype屬性所指向的原型對象,就返回true。

而prototype根本就不是實例具有的屬性(或者說實例的prototype屬性爲undefined),而是它原型對象中的屬性,如果被篡改了,這個判斷方法就不能正確返回了。

另外,能不能直接判斷 person.constructor == Person來取得想要的結果呢?

我們做個測試,如下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對象的構造函數,Number()用於將其參數轉換爲數字number類型,並返回轉換結果(若不能轉換則返回NaN)。

在JavaScript中constructor較少使用,variable.constructor返回其對象類的構造函數的字符串表示。

那麼在JavaScript中判斷數據類型時,我們可以使用以下方式來得到其詳細數據類型:

if((typeof a=="object") && (a.constructor==Array)){

}

注意:constructor只能對已有變量進行判斷,而typeof則可對未聲明變量或空對象進行判斷(返回undefined)。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章