沉沙
2018-11-16
来源 :
阅读 1344
评论 0
摘要:本篇教程介绍了JavaScript基础教程之typeof,希望阅读本篇文章以后大家有所收获,帮助大家对JavaScript的理解更加深入。
本篇教程介绍了JavaScript基础教程之typeof,希望阅读本篇文章以后大家有所收获,帮助大家对JavaScript的理解更加深入。
<
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‘;
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标WEB前端JavaScript频道!
喜欢 | 0
不喜欢 | 0
您输入的评论内容中包含违禁敏感词
我知道了

请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式AI+学习就业服务平台 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号