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); ...
随机推荐
- 使用 IntraWeb (12) - 基本控件之 TIWGradButton、TIWImageButton
TIWGradButton.TIWImageButton 分别是有颜色梯度变化按钮和图像按钮. TIWGradButton 所在单元及继承链: IWCompGradButton.TIWGradButt ...
- Eclipse 正则表达式 查找与替换
CTRL + space in the textboxes gives you all kinds of suggestions for regular expression writing. 查找 ...
- [Go] sync.Once 的用法
sync.Once.Do(f func()) 是一个非常有意思的东西,能保证 once 只执行一次,无论你是否更换 once.Do(xx) 这里的方法,这个 sync.Once块 只会执行一次. pa ...
- Android内存机制分析——堆和栈
昨天用Gallery做了一个图片浏览选择开机画面的功能,当我加载的图片多了就出现OOM问题.以前也出现过这个问题,那时候并没有深究.这次打算好好分析一下Android的内存机制. 因为我以前是做VC+ ...
- 如何:声明、实例化和使用委托(C# 编程指南)
委托的声明如下所示: C# public delegate void Del<T>(T item); public void Notify(int i) { } C# Del< ...
- android在代码中四种设置控件背景颜色的方法(包含RGB)
转载请注明出处: http://blog.csdn.net/fth826595345/article/details/9208771 TextView tText=(TextView) findVi ...
- UICollectionView 相关
当数据不多,collectionView.contentSize小于collectionView.frame.size的时候,UICollectionView是不会滚动的 self.Cov.alway ...
- 神奇女侠Wonder Woman迅雷下载
亚马逊公主戴安娜·普林斯(盖尔·加朵 Gal Gadot 饰),经过在家乡天堂岛的训练,取得上帝赐予的武器 与装备,化身神奇女侠,与空军上尉史蒂夫·特雷弗(克里斯·派恩 Chris Pine 饰)一同 ...
- 无耻之徒(美版)第一季/全集Shameless US迅雷下载
第一季 Shameless Season 1 (2011)看点:本以为美版<无耻之徒>(Shameless)是小众剧(诸多儿童不宜),但是试播集98.2万的收视人次竟然创下了Showtim ...
- 发展受阻第一至四季/全集Arrested迅雷下载
发展受阻 第一至四季 Arrested Development 1-4 (2013)本季看点:<发展受阻>讲述了一个很有钱的Bluth家庭的故事,主要聚焦家族里的各种古怪事和相互算计带来的 ...