typeof详解
typeof operator 返回了表示对象类型的字符串
下表列出了typeof可能的返回值。
| Type | Result |
|---|---|
| Undefined | "undefined" |
| Null | "object" |
| Boolean | "boolean" |
| Number | "number" |
| String | "string" |
| Symbol (ECMAScript 2015的新特性) | "symbol" |
| Host object (js环境提供) | 依赖于具体的浏览器环境 |
| Function object (implements [[Call]] in ECMA-262 terms) | "function" |
| Any other object | "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'; // but never use this form!
// Strings
typeof "" === 'string';
typeof "bla" === 'string';
typeof (typeof 1) === 'string'; // typeof永远返回string
typeof String("abc") === 'string'; // but never use this form!
// Booleans
typeof true === 'boolean';
typeof false === 'boolean';
typeof Boolean(true) === 'boolean'; // but never use this form!
// Symbols
typeof Symbol() === 'symbol'
typeof Symbol('foo') === 'symbol'
typeof Symbol.iterator === 'symbol'
// Undefined
typeof undefined === 'undefined';
typeof 声明了但是没有定义的变量=== 'undefined';
typeof没有声明的变量 === 'undefined';
// Objects
typeof {a:1} === 'object';
//使用Array.isArray或者Object.prototypr.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';
// Functions
typeof function(){} === 'function';
typeof class C {} === 'function';
typeof Math.sin === 'function';
null
//从js实现之初就存在
typeof null === 'object';
在最初JS的实现时,js值包括了一个值和一个类型标签(a type tag)。对象的类型标签是0。null是空指针,在大多数平台指向0x00。所以,null的类型标签也是0.因此,type null返回object。
Regular expressions
可调用的正则表达式(Callable regular expressions)在一些浏览器里不统一。
typeof /s/ === 'function'; // Chrome 1-12 和ECMAScript 5.1标准不一致
typeof /s/ === 'object'; // Firefox 5+ 符合ECMAScript 5.1标准
ie特性
在IE6-8中许多许多宿主对象typeof之后返回的是object而不是function 。例如:
typeof alert === 'object'
typeof详解的更多相关文章
- C语言typeof详解 offsetof
http://blog.chinaunix.net/uid-28458801-id-4200573.html 前言: typeof关键字是C语言中的一个新扩展,这个特性在linux内核中应用非常 ...
- C语言typeof详解
前言: typeof关键字是C语言中的一个新扩展,这个特性在linux内核中应用非常广泛. 一,说明 typeof的参数可以是两种形式:表达式或类型. 1,表达式的的例子: ...
- JavaScript中typeof详解
[范围]typeof返回值范围: typeof返回值对应 类型 结果 String "string" Number "number" Boolean " ...
- JS 中 判断数据类型 typeof详解
typeof 可用来获取检测变量的数据类型 语法 typeof operand typeof(operand) 参数 operand 一个表示对象或原始值的表达式,其类型将被返回. 描述 下表总结 ...
- 原生JS:delete、in、typeof、instanceof、void详解
delete.in.typeof.instanceof.void详解 本文参考MDN做的详细整理,方便大家参考[MDN](https://developer.mozilla.org/zh-CN/doc ...
- JavaScript中typeof和instanceof深入详解
这次主要说说javascript的类型判断函数typeof和判断构造函数原型instanceof的用法和注意的地方. typeof 先来说说typeof吧.首先需要注意的是,typeof方法返回一个字 ...
- Net is as typeof 运行运算符详解 net 自定义泛型那点事
Net is as typeof 运行运算符详解 概述 在了解运行运算符的前提我们需要了解什么是RTTI ,在任何一门面向对象的语言中,都有RTTI这个概念(即 运行时). RTTI(Run-Ti ...
- C#进阶系列——WebApi 接口返回值不困惑:返回值类型详解
前言:已经有一个月没写点什么了,感觉心里空落落的.今天再来篇干货,想要学习Webapi的园友们速速动起来,跟着博主一起来学习吧.之前分享过一篇 C#进阶系列——WebApi接口传参不再困惑:传参详解 ...
- Xamarin.Android通知详解
一.发送通知的机制 在日常的app应用中经常需要使用通知,因为服务.广播后台活动如果有事件需要通知用户,则需要通过通知栏显示,而在Xamarin.Android下的通知需要获取Notification ...
随机推荐
- JavaScript中的Math方法演示
<html> <head> <script type="text/javascript"> var num = 12.4; alert(Math ...
- 小谈Vim打开文件开头的<feff>
在本地Windows机上开发的PHP程序上传到linuxserver上后,通过浏览器訪问对应接口.发现返回的数据前多了一个莫名的字符'-',甚为不解.之后通过网络抓包的方式,查看到接口返回数据前多了 ...
- C++虚继承的概念(转)
http://blog.csdn.net/wangxingbao4227/article/details/6772579 C++中虚拟继承的概念 为了解决从不同途径继承来的同名的数据成员在内存中有不同 ...
- python(38)- 网络编程socket
一 客户端/服务器架构 即C/S架构,包括 1.硬件C/S架构(打印机) 2.软件C/S架构(web服务) 美好的愿望: 最常用的软件服务器是 Web 服务器.一台机器里放一些网页或 Web 应用程序 ...
- 【每日Scrum】第五天(4.26) TD学生助手Sprint2站立会议
站立会议 组员 昨天 今天 困难 签到 刘铸辉 (组长) 今天增加了几个页面的子菜单,然后设计了几个要用的界面 今天和楠哥做了课程事件和日历表操作的例子,并尝试做时间表和日历表的数据库设计 安卓的数据 ...
- (9)launcher3 之 外部 更换主题Theme APP demo 实现原理以及demo
先说下我的思路: luancher3里面更换图标的逻辑例如以下: 先从APP资源包里查询--数据库查询--其它地方查询ICON 因此,我们仅仅须要把 从数据库获取ICON 代码提前到 从APP资源包 ...
- IDEA搭建Android wear开发环境,Android wear,I'm comming!
随着google公布了android wear这个东西.然后又有了三星的gear,LG的G watch以及moto 360,苹果由公布了apple watch.未来可能在智能手表行业又有一场战争. 当 ...
- 区分拖曳(drag)和点击(click)事件
假设页面上有一个a标签: <a href="http://www.google.com">google</a> 现在需要对这个标签进行拖放操作,会发现当拖曳 ...
- Spring Boot外部化配置实战解析
一.流程分析 1.1 入口程序 在 SpringApplication#run(String... args) 方法中,外部化配置关键流程分为以下四步 public ConfigurableAppli ...
- 图像处理之滤波---gabor
http://blog.csdn.net/xiaowei_cqu/article/details/24745945 小魏北大