typeof操作符返回一个字符串,表示未经计算的操作数的类型。
typeof
操作符返回一个字符串,表示未经计算的操作数的类型。
语法
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" |
示例
// 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
// 区分数组,普通对象
typeof [1, 2, 4] === 'object';
typeof new Date() === 'object';
// 下面的容易令人迷惑,不要使用!
typeof new Boolean(true) === 'object';
typeof new Number(1) === 'object';
typeof new String("abc") === 'object';
// 函数
typeof function(){} === 'function';
typeof class C{} === 'function'
typeof Math.sin === 'function';
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
暂存死区
在 ECMAScript 2015 之前,typeof
总是保证为任何操作数返回一个字符串。但是,除了非提升,块作用域的let和const之外,在声明之前对块中的let
和const
变量使用typeof
会抛出一个ReferenceError。这与未声明的变量形成对比,typeof
会返回“undefined”。块作用域变量在块的头部处于“暂时死区”,直到被初始化,在这期间,如果变量被访问将会引发错误。
typeof undeclaredVariable === 'undefined';
typeof newLetVariable; let newLetVariable; // ReferenceError
typeof newConstVariable; const newConstVariable = 'hello'; // ReferenceError
例外
所有当前的浏览器都暴露了一个类型为 undefined 的非标准宿主对象 document.all
。
typeof document.all === 'undefined';
尽管规范允许为非标准的外来对象定制类型标签,但它要求这些类型标签与预定义标签不同。document.all
的类型标记为“undefined
”的情况必须被列为违反规则的特殊情况
typeof操作符返回一个字符串,表示未经计算的操作数的类型。的更多相关文章
- SqlSever基础 len函数 返回一个字符串的长度
镇场诗:---大梦谁觉,水月中建博客.百千磨难,才知世事无常.---今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ ...
- JAVA传入一个字符串,返回一个字符串中的大写字母
/** * * @param 传入一个字符串 * @return 返回一个字符串中的大写字母 */ private static String str ...
- 令assignment操作符返回一个reference to *this
[令assignment操作符返回一个reference to *this] 关于赋值,可以把它们写成连锁形式: int x, y, z; x =y =z =15; II赋值连锁形式 上述连锁赋值被解 ...
- struts2怎么返回一个字符串给jsp?(使用json)
我们都知道使用servlet时可以直接用PrintWriter对象的print方法来向页面传送一些字符串(可以是html标签和内容),然后在用RequestDispatcher来转向网页 虽Strut ...
- 解决springmvc在单纯返回一个字符串对象时所出现的乱码情况(极速版)
使用springmvc框架开发了这么长时间,之前都是直接返回jsp页面,乱码情况都是通过配置和手动编解码来解决,但是今天突然返回一段单纯的字符串时,发现中文乱码情况解决不了了,下面就给各位分享一下如何 ...
- typeof操作符 返回值
Type操作符 返回值 : 1undefined 这个未定义 2.boolean 这个为boolean类型 3.string 这个是字符串 4.number 这个就是数值 5 ...
- [Effective C++ --010]令赋值操作符返回一个reference to *this
差不多最经典的就是这个了: x = y = z = ; 解读为: x = (y = ( z = )); 如果没有返回值,上述代码就不能通过编译. 其实看到标题就差不多明白这一条了,但是为什么连续赋值时 ...
- python1.返回一个字符串中出现次数第二多的单词 2.字符串中可能有英文单词、标点、空格 3.字符串中的英文字符全部是小写
import re from collections import Counter def second_count_word(s): # # 利用正则按标点和空格切割,有其他标点可以添加到[]内 # ...
- JS返回一个字符串中长度最小的单词的长度
题目:编写一个方法,返回字符串中最小长度的单词的长度. var str = 'What a good day today!'; 1 //方法一 2 function returnString1(str ...
随机推荐
- jmeter自定义并发用户数图形插件介绍
Stepping Thread Group马上要被废弃了,废弃原因不知道,官方推荐使用 BlazeMeter Inc.公司贡献的插件Concurrency Thread Group,配合 Throug ...
- Raspberry Pi学习笔记
一.树莓派 Raspberry Pi 更换国内源 编辑 /etc/apt/sources.list 文件,用 nano 命令编辑 pi@raspberrypi:~$ sudo cp /etc/apt/ ...
- [leetcode解题记录]Jump Game和Jump Game II
Jump Game Given an array of non-negative integers, you are initially positioned at the first index o ...
- Comparable 和 Comparator的理解
对Comparable 的解释 Comparable是一个排序接口 此接口给实现类提供了一个排序的方法,此接口有且只有一个方法 public int compareTo(T o); compareTo ...
- 常用shell命令的写法
这并不是教人怎么进行shell编程的文章,只是韦哥在工作中用到的一些简单脚本的写法.因为有些命令即使用过几次了,再次使用时仍然写不对,需要man来看下或者需要google,你也可以理解为对命令的理解不 ...
- Sublime Text2-Control Package---ShinePans
1.打开sublime Text2 2.菜单条中的preference>>BrowsePackages 3.退到上一级打开Installed Packages 4.拷贝文件到此目录 (Pa ...
- WCF服务端的.NET Core支持项目Core WCF 正式启动
长期以来在wcf客户端库 https://github.com/dotnet/wcf 里反应最强烈的就是.NET Core的服务端支持 https://github.com/dotnet/wcf/is ...
- Spark 学习笔记:(三)Spark SQL
参考:https://spark.apache.org/docs/latest/sql-programming-guide.html#overview http://www.csdn.net/arti ...
- 中断线程Interrupt()
以下是参考<<Java多线程模式>>的 1. sleep() & interrupt() 线程A正在使用sleep()暂停着: Thread.sleep(100000) ...
- HBase协处理器同步二级索引到Solr
一. 背景二. 什么是HBase的协处理器三. HBase协处理器同步数据到Solr四. 添加协处理器五. 测试六. 协处理器动态加载 一. 背景 在实际生产中,HBase往往不能满足多维度分析,我们 ...