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.toString
with 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); ...
随机推荐
- j.u.c系列(09)---之并发工具类:CyclicBarrier
写在前面 CyclicBarrier是一个同步辅助类,允许一组线程互相等待,直到到达某个公共屏障点 (common barrier point).因为该 barrier 在释放等待线程后可以重用,所以 ...
- 使用 Python 登录网站(转)
对于大部分论坛,我们想要抓取其中的帖子分析,首先需要登录,否则无法查看. 这是因为 HTTP 协议是一个无状态(Stateless)的协议,服务器如何知道当前请求连接的用户是否已经登录了呢?有两种方式 ...
- IDA .edata .rdata .idata .text segments
.rdata is for const data. It is the read only version of the .data segment. .idata holds the import ...
- golang 引用相对路径package
My $GOPATH is "/Users/peter/goworkspace" Golang 版本是: go version go1.6 darwin/amd64 在这个work ...
- C#基于SMTP协议和SOCKET通信,实现邮件内容和附件的发送,并可隐藏收件人
经过几天的努力,从完全不懂SMTP到折腾出个可以发送邮件内容和附件的DEMO.话少说,直接上代码. using System; using System.Collections.Generic; us ...
- leetcode——169 Majority Element(数组中出现次数过半的元素)
Given an array of size n, find the majority element. The majority element is the element that appear ...
- Python 中函数的 收集参数 机制
定义函数的时候,在参数前加了一个 * 号,函数可以接收零个或多个值作为参数.返回结果是一个元组. 传递零个参数时函数并不报错,而是返回一个空元组.但以上这种方法也有局限性,它不能收集关键字参数. 对关 ...
- 3I工作室的成员在2013年(第6届)中国大学生计算机设计大赛总决赛中荣获全国二等奖
在暑假举行的2013年(第6届)中国大学生计算机设计大赛中,我院的参赛作品<毕业生论文选导系统>(作者:祝丽艳/许明涛:指导老师:元昌安/彭昱忠)入围总决赛,并荣获全国二等奖. 2013年 ...
- IE中div被视频遮住的解决方法
使用embed来内嵌视频,因为视频是windows media player,上面想用div浮动一些内容,之前尝试了一些方法,比如 1. 通过设定不同组件的z-index值 2. 通过设定 wmode ...
- arcengine Annotation研究的一些学习资料(转)FeatureWeight
转自chanyinhelv原文Annotation研究的一些学习资料 下面是我最近对Annotation研究的一些学习资料,收集于此,供大家学习之用. 一.Annotation要素类介绍 在GeoDa ...