JavaScript 之 typeof

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 operand
or
typeof (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 // Strings
10 typeof "" === 'string';
11 typeof "bla" === 'string';
12 typeof (typeof 1) === 'string'; // typeof总是返回一个字符串
13 typeof String("abc") === 'string'; // 但不要使用这种形式!
14 
15 // Booleans
16 typeof true === 'boolean';
17 typeof false === 'boolean';
18 typeof Boolean(true) === 'boolean'; // 但不要使用这种形式!
19 
20 // Symbols
21 typeof Symbol() === 'symbol';
22 typeof Symbol('foo') === 'symbol';
23 typeof Symbol.iterator === 'symbol';
24 
25 // Undefined
26 typeof undefined === 'undefined';
27 typeof declaredButUndefinedVariable === 'undefined';
28 typeof undeclaredVariable === 'undefined'; 
29 
30 // Objects
31 typeof {a:1} === 'object';
32 
33 // 使用Array.isArray 或者 Object.prototype.toString.call
34 // 区分数组,普通对象
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 代表的是空指针(大多数平台下值为 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 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

暂存死区

typeof undeclaredVariable === 'undefined';
typeof newLetVariable; let newLetVariable; // ReferenceError
typeof newConstVariable; const newConstVariable = 'hello'; // ReferenceError

例外

所有当前的浏览器都暴露了一个类型为 undefined 的非标准宿主对象 document.all

typeof document.all === 'undefined';

参照来源:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/typeof

原文地址:https://www.cnblogs.com/xianfengzhike/p/9168997.html