標籤:ase 判斷 拒絕 錯誤 error body instant ref ocs
typeof操作符返回一個字串,表示未經計算的運算元的類型。
JavaScript Demo: Expressions - typeof
console.log(typeof 42);// expected output: "number"console.log(typeof ‘blubber‘);// expected output: "string"console.log(typeof true);// expected output: "boolean"console.log(typeof declaredButUndefinedVariable);// expected output: "undefined";
文法
typeof運算子後跟運算元:
typeof operandortypeof (operand)
參數
operand 是一個運算式,表示對象或原始值,其類型將被返回。
括弧是可選的。
描述
下表總結了typeof可能的傳回值。有關類型和原始值的更多資訊,可查看 JavaScript資料結構 頁面。
| 類型 |
結果 |
| Undefined |
"undefined" |
| Null |
"object"(見下文) |
| Boolean |
"boolean" |
| Number |
"number" |
| String |
"string" |
| Symbol (ECMAScript 6 新增) |
"symbol" |
| 宿主對象(由JS環境提供) |
Implementation-dependent |
| 函數對象([[Call]] 在ECMA-262條款中實現了) |
"function" |
| 任何其他對象 |
"object" |
樣本
1 // Numbers 2 typeof 37 === ‘number‘; 3 typeof 3.14 === ‘number‘; 4 typeof Math.LN2 === ‘number‘; 5 typeof Infinity === ‘number‘; 6 typeof NaN === ‘number‘; // 儘管NaN是"Not-A-Number"的縮寫 7 typeof Number(1) === ‘number‘; // 但不要使用這種形式! 8 9 // Strings10 typeof "" === ‘string‘;11 typeof "bla" === ‘string‘;12 typeof (typeof 1) === ‘string‘; // typeof總是返回一個字串13 typeof String("abc") === ‘string‘; // 但不要使用這種形式!14 15 // Booleans16 typeof true === ‘boolean‘;17 typeof false === ‘boolean‘;18 typeof Boolean(true) === ‘boolean‘; // 但不要使用這種形式!19 20 // Symbols21 typeof Symbol() === ‘symbol‘;22 typeof Symbol(‘foo‘) === ‘symbol‘;23 typeof Symbol.iterator === ‘symbol‘;24 25 // Undefined26 typeof undefined === ‘undefined‘;27 typeof declaredButUndefinedVariable === ‘undefined‘;28 typeof undeclaredVariable === ‘undefined‘; 29 30 // Objects31 typeof {a:1} === ‘object‘;32 33 // 使用Array.isArray 或者 Object.prototype.toString.call34 // 區分數組,普通對象35 typeof [1, 2, 4] === ‘object‘;36 37 typeof new Date() === ‘object‘;38 39 // 下面的容易令人迷惑,不要使用!40 typeof new Boolean(true) === ‘object‘;41 typeof new Number(1) === ‘object‘;42 typeof new String("abc") === ‘object‘;43 44 // 函數45 typeof function(){} === ‘function‘;46 typeof class C{} === ‘function‘47 typeof Math.sin === ‘function‘;48 typeof new Function() === ‘function‘;
null
typeof null === ‘object‘; // 從一開始出現JavaScript就是這樣的
在 JavaScript 最初的實現中,JavaScript 中的值是由一個表示類型的標籤和實際資料值表示的。對象的類型標籤是 0。由於 null 代表的是null 指標(大多數平台下值為 0x00),因此,null的類型標籤也成為了 0,typeof null就錯誤的返回了"object"。(reference)
ECMAScript提出了一個修複(通過opt-in),但被拒絕。這將導致typeof null === ‘object‘。
使用
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 Javascriptvar 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‘
Regex
對Regex字面量的類型判斷在某些瀏覽器中不符合標準:
typeof /s/ === ‘function‘; // Chrome 1-12 , 不符合 ECMAScript 5.1typeof /s/ === ‘object‘; // Firefox 5+ , 符合 ECMAScript 5.1
暫存死區
typeof undeclaredVariable === ‘undefined‘;typeof newLetVariable; let newLetVariable; // ReferenceErrortypeof newConstVariable; const newConstVariable = ‘hello‘; // ReferenceError
例外
所有當前的瀏覽器都暴露了一個類型為 undefined 的非標準宿主對象 document.all。
typeof document.all === ‘undefined‘;
參照來源:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/typeof
JavaScript 之 typeof