DIV CSS 佈局教程網

淺談 JavaScript 中的型別判斷
編輯:關於JavaScript     

資料型別的判斷方式大致有以下幾種:

1、一元運算符 typeof
2、關係運算符 instanceof
3、constructor 屬性
4、Object.prototype.toString

一、typeof

typeof 的返回值有:undefined 對應 "undefined";null 對應 "object";布林值對應 "boolean";數值對應 "number";字串對應 "string";Symbol(ES6 新增)對應 "symbol";函式對象對應 "function";任何其他對象對應 "object"。

// 以下程式碼在 Google Chrome 45.0.2454.101 m 中測試通過
// Numbers
console.log(typeof 37 === 'number');
console.log(typeof 3.14 === 'number');
console.log(typeof NaN === 'number'); // 盡管 NaN 是 "Not-A-Number" 的縮寫
// Strings
console.log(typeof "" === 'string');
console.log(typeof (typeof 1) === 'string');
// Booleans
console.log(typeof true === 'boolean');
// Symbols
console.log(typeof Symbol() === 'symbol');
// Undefined
console.log(typeof undefined === 'undefined');
console.log(typeof blabla === 'undefined');
// Objects(使用 Array.isArray 或 Object.prototype.toString.call 可從基本對象中區分出陣列)
console.log(typeof {a:1} === 'object');
console.log(typeof [1, 2, 4] === 'object');
console.log(typeof null === 'object'); // true
// 函數
console.log(typeof function(){} === 'function');
console.log(typeof Math.sin === 'function');

typeof 只能檢測出以上幾種類型。

二、instanceof

instanceof 運算符用於識別正在處理的對象的類型,要求開發者明確確認對象為某特定類型。它又叫關係運算符,可用來判斷某個構造函式的 prototype 屬性是否存在於另一個檢測對象的原型鏈上。

var str = new String("hello world");
console.log(str instanceof String);// true
console.log(String instanceof Function);// true
console.log(str instanceof Function);// false

instanceof 與 constructor 沒有直接關係,判斷依據是原型鏈:

var A = function() {};
var a = new A();
A.prototype = {};
var b = new A();
console.log(a instanceof A);// false
console.log(b instanceof A);// true

一些容易令人困惑的情況:

console.log(Object instanceof Object);// true
console.log(Function instanceof Function);// true
console.log(Number instanceof Number);// false
console.log(String instanceof String);// false
console.log(Function instanceof Object);// true

要理解這些結果,需要明白兩點:語言規範中對該運算符的定義,以及 JavaScript 的原型繼承機制。例如 Object instanceof Object 為 true,是因為 Object.__proto__ === Function.prototype,而 Function.prototype.__proto__ === Object.prototype。

另外,在多視窗(多個 frame 或多個 window)環境中,不同全局環境擁有不同的內建型別構造函式,[] instanceof window.frames[0].Array 會返回 false,此時應使用 Array.isArray(myObj) 或 Object.prototype.toString.call(myObj) === "[object Array]" 來判斷。

注意:instanceof 無法檢測出 number、string、boolean 這些基本型別的類型。

三、constructor

Object.prototype.constructor 返回一個指向創建了該對象原型的函式的引用,其值是函式本身而非字串。所有對象都會從它的原型上繼承一個 constructor 屬性。

function Person(){ }
var Tom = new Person();
console.log(Tom.constructor === Person);// true

不過 constructor 屬性是可以被修改的,會導致檢測結果不正確:

function Person(){ }
function Student(){ }
Student.prototype = new Person();
var John = new Student();
console.log(John.constructor==Student); // false
console.log(John.constructor==Person); // true

除了 undefined 和 null,其他類型的變數均能使用 constructor 判斷出類型。

四、萬能的 Object.prototype.toString.call

var toString = Object.prototype.toString;
console.log(toString.call(new Date) === '[object Date]');// true
console.log(toString.call(new String) ==='[object String]');// true
console.log(toString.call(new Function) ==='[object Function]');// true
console.log(toString.call(Math) === '[object Math]');// true
console.log(toString.call(/^[a-zA-Z]{5,20}$/) ==='[object RegExp]');// true
console.log(toString.call([1, 2, 3, 4]) ==='[object Array]');// true
// Since JavaScript 1.8.5
console.log(toString.call(undefined) === '[object Undefined]');// true
console.log(toString.call(null) === '[object Null]');// true

五、jQuery 的實現

jQuery 提供了一個 $.type 的接口,其核心正是用 Object.prototype.toString.call 來實現:

var class2type = {};
var toString = Object.prototype.toString;
type: function(a) {
  return a == null ? String(a) : class2type[toString.call(a)] || "object";
}

由此可見,jQuery 中就是用 Object.prototype.toString.call 來實現型別判斷的。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved