typeof操作符
typeof操作符返回一個字符串辱姨,表示未經(jīng)計算的操作數(shù)的類型
typeof operand
typeof (operand)
類型 | 結(jié)果 |
---|---|
undefined | "undefined" |
null | "object" |
Boolean | "boolean" |
Number | "number" |
String | "string" |
Symbol | "symbol" |
宿主對象(Js環(huán)境提供) | Implementation-dependent |
函數(shù)對象 | "function" |
其他對象 | "object" |
// Numbers
typeof 37 === 'number';
typeof 3.14 === 'number';
typeof Math.LN2 === 'number';
typeof Infinity === 'number';
typeof NaN === 'number'; // 盡管NaN是"Not-A-Number"的縮寫
typeof Number(1) === 'number'; // 但不要使用這種形式!
// Strings
typeof "" === 'string';
typeof "bla" === 'string';
typeof (typeof 1) === 'string'; // typeof總是返回一個字符串
typeof String("abc") === 'string'; // 但不要使用這種形式!
// Booleans
typeof true === 'boolean';
typeof false === 'boolean';
typeof Boolean(true) === 'boolean'; // 但不要使用這種形式!
// Symbols
typeof Symbol() === 'symbol';
typeof Symbol('foo') === 'symbol';
typeof Symbol.iterator === 'symbol';
// Undefined
typeof undefined === 'undefined';
typeof declaredButUndefinedVariable === 'undefined';
typeof undeclaredVariable === 'undefined';
// Objects
typeof {a:1} === 'object';
// 使用Array.isArray 或者 Object.prototype.toString.call
// 區(qū)分數(shù)組,普通對象
typeof [1, 2, 4] === 'object';
typeof new Date() === 'object';
// 下面的容易令人迷惑,不要使用戚嗅!
typeof new Boolean(true) === 'object';
typeof new Number(1) === 'object';
typeof new String("abc") === 'object';
// 函數(shù)
typeof function(){} === 'function';
typeof class C{} === 'function'
typeof Math.sin === 'function';
typeof new Function() === 'function';
null
在 JavaScript 最初的實現(xiàn)中雨涛,JavaScript 中的值是由一個表示類型的標簽和實際數(shù)據(jù)值表示的。對象的類型標簽是 0懦胞。由于 null 代表的是空指針(大多數(shù)平臺下值為 0x00)替久,因此,null的類型標簽也成為了 0躏尉,typeof null就錯誤的返回了"object"蚯根。(reference)
使用new操作符
// All constructor functions while instantiated with 'new' keyword will always be typeof 'object'
var str = new String('String');
var num = new Number(100);
typeof str; // It will return 'object'
typeof num; // It will return 'object'
// But there is a exception in case of Function constructor of Javascript
var func = new Function();
typeof func; // It will return 'function'
語法中的括號
// Parentheses will be very much useful to determine the data type for expressions.
var iData = 99;
typeof iData + ' Wisen'; // It will return 'number Wisen'
typeof (iData + ' Wisen'); // It will return 'string'
正則表達式
對正則表達式字面量的類型判斷在某些瀏覽器中不符合標準:
typeof /s/ === 'function'; // Chrome 1-12 , 不符合 ECMAScript 5.1
typeof /s/ === 'object'; // Firefox 5+ , 符合 ECMAScript 5.1
暫存死區(qū)
塊級作用域 let const,在聲明之前進行typeof操作會拋出一個ReferenceError胀糜。塊級作用域在塊的頭部處于"暫時性死區(qū)“颅拦,知道被初始化,在這期間訪問變量會引發(fā)錯誤教藻。
例外
typeof document.all === 'undefined'
IE
在IE 6,7和8上距帅,很多宿主對象是對象而不是函數(shù)。例如:
typeof alert === 'object'
常見問題
1. typeof 的返回值總共有多少個括堤?
答:"number","string","boolean","undefined","object","function","symbol"這7個返回值
2. typeof 為什么要區(qū)分object和function碌秸?
答:函數(shù)在ECMAScript中是對象,不是一種數(shù)據(jù)類型悄窃,函數(shù)還包含一些特殊屬性哮肚,因此有必要使用typeof來區(qū)分。實際使用過程中有這個需求广匙。
3.typeof的不足之處有哪些
答:typeof操作符不能準確的區(qū)分對象允趟,數(shù)組,正則鸦致。返回值為"object"潮剪。部分早期瀏覽器版本對正則對象返回"function"涣楷。IE67中typeof alert //object,其他瀏覽器type alert //function
4.判斷下列表達式的值
typeof 1/0 //NaN
typeof (1/0) //number
typeof typeof 1/0 // NaN
typeof typeof (1/0) // string
typeof (typeof 1/0) // number
5.用typeof來判斷對象的潛在陷阱是什么
答:JavaScript對于typeof []==='object'
,typeof null === 'object'
,typeof function(){}==='function'
,所以在使用typeof來做判斷時需要考慮這些情況抗碰。當然也可以選擇替代方法狮斗,Object.prototype.toString.call([])
來進行判斷,返回值類似于[object Array]
,可以加上正則處理為 Object.prototype.toString.call([]]).replace(/\[object\s|\]/g,'')
參考資料:
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/typeof