Fixing the JavaScript typeof operator
https://javascriptweblog.wordpress.com/2011/08/08/fixing-the-javascript-typeof-operator/
javascript 类型判断函数
var toType = function(obj) {
return ({}).toString.call(obj).match(/\s([a-zA-Z]+)/)[1].toLowerCase()
}
A Better Way?
[[Class]]
Every JavaScript object has an internal property known as [[Class]] (The ES5 spec uses the double square bracket notation to represent internal properties, i.e. abstract properties used to specify the behavior of JavaScript engines). According to ES5, [[Class]] is “a String value indicating a specification defined classification of objects”. To you and me, that means each built-in object type has a unique non-editable, standards-enforced value for its [[Class]] property. This could be really useful if only we could get at the [[Class]] property…
Object.prototype.toString
…and it turns out we can. Take a look at the ES 5 specification for Object.prototype.toString…
- Let O be the result of calling ToObject passing the this value as the argument.
- Let class be the value of the [[Class]] internal property of O.
- Return the String value that is the result of concatenating the three Strings
"[object ", class, and"]".
In short, the default toString function of Object returns a string with the following format…
[object [[Class]]]
…where [[Class]] is the class property of the object.
Unfortunately, the specialized built-in objects mostly overwrite Object.prototype.toStringwith toString methods of their own…
[1,2,3].toString(); //"1, 2, 3" (new Date).toString(); //"Sat Aug 06 2011 16:29:13 GMT-0700 (PDT)" /a-z/.toString(); //"/a-z/"
…fortunately we can use the call function to force the generic toString function upon them…
Object.prototype.toString.call([1,2,3]); //"[object Array]" Object.prototype.toString.call(new Date); //"[object Date]" Object.prototype.toString.call(/a-z/); //"[object RegExp]"
Introducing the toType function
We can take this technique, add a drop of regEx, and create a tiny function – a new and improved version of the typeOf operator…
Fixing the JavaScript typeof operator的更多相关文章
- [TypeScript] Use the JavaScript “in” operator for automatic type inference in TypeScript
Sometimes we might want to make a function more generic by having it accept a union of different typ ...
- 细说javascript typeof操作符
细说javascript typeof操作符 typeof定义 typeof是一元运算符,用来返回操作数类型的字符串.下面是ECAMScript5.1关于typeof的标准定义: NOTE:上面表格标 ...
- javascript typeof 和 constructor比较
转自:http://www.cnblogs.com/hacker84/archive/2009/04/22/1441500.html http://www.cnblogs.com/siceblue/a ...
- JavaScript typeof function()的注意事项
首先,上一段代码: var f = function g() { return 23; }; console.log(typeof g); //输出undefined //console.log(ty ...
- JavaScript typeof, null, 和 undefined
typeof 操作符 你可以使用 typeof 操作符来检测变量的数据类型. 实例 typeof "John" // 返回 string typeof ...
- javascript:typeof与instanceof区别
from:http://www.wxwdesign.cn/article/skills/javascript_typeof_instanceof.htm JavaScript中typeof和insta ...
- Javascript typeof 用法
在js里用到数组,比如 多个名字相同的input, 若是动态生成的, 提交时就需要判断其是否是数组. if(document.mylist.length != "undefined" ...
- javascript typeof 和 instanceof 的区别和联系
这篇文章是我看完<JavaScript高级程序设计(第2版)>书籍的随笔文章,目的只有一个,以备自己和网友们方便参考和记忆! typeof是什么? typeof 是一个操作 ...
- JavaScript typeof运算符和数据类型
// js有6种数据类型:Undefined.Null.Boolean.String.Number.Object //(01)typeof console.log(typeof undefined); ...
随机推荐
- mysql慢查询日志功能的使用
作用:mysql慢查询日志可监控有效率问题的SQL .. 一.开启mysql慢查询日志功能 1.查看是否开启 未使用索引的SQL记录日志查询 mysql> show variables like ...
- ftp通用类1
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.N ...
- RDMA over TCP的协议栈工作过程浅析
http://blog.chinaunix.net/uid-140205-id-2849342.html
- C++11 function
#include <iostream> #include <functional> #include <vector> using namespace std; / ...
- Linux内存管理学习1 —— head.S中的段页表的建立
作者 彭东林 pengdonglin137@163.com 平台 TQ2440 Qemu+vexpress-ca9 Linux-4.10.17 概述 在Linux自解压完毕后,开始执行arch/arm ...
- 使用Axure RP原型设计实践03,制作一个登录界面的原型
本篇体验做一个登录界面的原型. 登录页 首先在Page Style里为页面设置背景色. 如果想在页面中加图片,就把Image部件拖入页面,并设置x和y轴.双击页面中的Image部件可以导入图片.在Im ...
- Java Jackson - Json Polymorphism
from://http://www.studytrails.com/java/json/java-jackson-Serialization-polymorphism.jsp Jackson prov ...
- Cocos2d-x之Schedule
Cocos2dx的定时器 from://http://blog.linguofeng.com/archive/2012/11/14/cocos2d-x-Schedule.html 一.schedule ...
- Java 如何实现在线预览文档及修改(文本文件)
暂时未解决的问题:多用户并发修改一个文件 测试地址: http://sms.reyo.cn 用户名:aa 密码:123456
- 破产姐妹第一季/全集2 Broke Girls迅雷下载
本季2 Broke Girls Season 1 (2011)看点:黑发泼辣的Max(凯特·戴琳斯 Kat Dennings 饰)在纽约布鲁克林区一家低档餐馆打工,餐馆同事包括小个子亚裔老板Han L ...