通过 Object.prototype.toString.call() 进行类型判断
2012-04-24 17:50
851 查看
首先看一段ECMA中对Object.prototype.toString的解释:
Object.prototype.toString( )
When the toString method is called, the following steps are taken:
1. Get the [[Class]] property of this object.
2. Compute a string value by concatenating the three strings “[object “, Result (1), and “]”.
3. Return Result (2)
我们知道,Javascript中,一切皆为对象。所以如下代码,应当会输出对应字符:
标准浏览器中完美的作到,但是(为什么要说但是呢)IE6中,却会出现以下问题:
通过Object.prototype.toString.call获取的 字符串,undefined,null均为Object
所以,我们又要悲剧的先对以上类型进行判断,完整代码:
Object.prototype.toString( )
When the toString method is called, the following steps are taken:
1. Get the [[Class]] property of this object.
2. Compute a string value by concatenating the three strings “[object “, Result (1), and “]”.
3. Return Result (2)
我们知道,Javascript中,一切皆为对象。所以如下代码,应当会输出对应字符:
var oP = Object.prototype, toString = oP.toString; console.log(toString.call([123]));//[object Array] console.log(toString.call('123'));//[object String] console.log(toString.call({a: '123'}));//[object Object] console.log(toString.call(/123/));//[object RegExp] console.log(toString.call(123));//[object Number] console.log(toString.call(undefined));//[object Undefined] console.log(toString.call(null));//[object Null] //....
标准浏览器中完美的作到,但是(为什么要说但是呢)IE6中,却会出现以下问题:
通过Object.prototype.toString.call获取的 字符串,undefined,null均为Object
所以,我们又要悲剧的先对以上类型进行判断,完整代码:
var oP = Object.prototype, toString = oP.toString; function typeOf(value) { if (null === value) { return 'null'; } var type = typeof value; if ('undefined' === type || 'string' === type) { return type; } var typeString = toString.call(value); switch (typeString) { case '[object Array]': return 'array'; case '[object Date]': return 'date'; case '[object Boolean]': return 'boolean'; case '[object Number]': return 'number'; case '[object Function]': return 'function'; case '[object RegExp]': return 'regexp'; case '[object Object]': if (undefined !== value.nodeType) { if (3 == value.nodeType) { return (/\S/).test(value.nodeValue) ? 'textnode': 'whitespace'; } else { return 'element'; } } else { return 'object'; } default: return 'unknow'; } }
相关文章推荐
- 通过 Object.prototype.toString.call() 进行类型判断
- 通过 Object.prototype.toString.call() 进行类型判断
- Object.prototype.toString.call()进行类型判断
- Object.prototype.toString.call() 进行类型判断
- js进行类型判断 Object.prototype.toString.call()
- 【JavaScript】Object.prototype.toString.call()进行类型判断
- Object.prototype.toString.call()进行类型判断
- Object.prototype.toString.call()进行类型判断
- 通过Object.prototype.toString.call()--判断数据(基本、引用)类型
- js中通过Object.prototype.toString方法----精确判断对象的类型
- JavaScript通过toString.call(value)进行数据类型判断
- Object.prototype.toString.call() 区分对象类型(判断对象类型)
- js中通过Object.prototype.toString方法----精确判断对象的类型
- js判断对象类型 typeof与Object.prototype.toString.call方法的区别
- js中通过Object.prototype.toString方法----精确判断对象的类型
- 判断一个变量的类型Object.prototype.toString.call
- js中通过Object.prototype.toString方法----精确判断对象的类型
- js中通过Object.prototype.toString方法----精判断对象的类型
- typeof 和 Object.prototype.toString.call 数据类型判断的区别
- js中通过Object.prototype.toString方法----精确判断对象的类型