JavaScript类型检测小结(2)
回归简单:Object.toString
这个方法并不新奇,在犀牛书的 9.7 Determining Object Type 一节中,有详细的讨论,但一直没有引起注意(犀牛书太厚,仔细阅读过的人,在世界范围内看来都是凤毛麟角的)。直到老道(Douglas Crockford)的火星文出现:The Miller Device(号召大家都向老道学习,多挖掘有价值的火星知识):
Object.prototype.toString.apply(value) === '[object Array]'
ECMA-262中的解释:
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)
这样,就有了:
function isArray(o) { return Object.prototype.toString.call(o) === '[object Array]'; }
因为是字符串比较,也就解决了跨iframe的问题。
结合typeof和toString方法,可以比较完美的实现对JavaScript基本类型和内置对象的探测了:
var _toS = Object.prototype.toString, _types = { 'undefined' : 'undefined', 'number' : 'number', 'boolean' : 'boolean', 'string' : 'string', '[object Function]' : 'function', '[object RegExp]' : 'regexp', '[object Array]' : 'array', '[object Date]' : 'date', '[object Error]' : 'error' }; function type(o) { return _types[typeof o] || _types[_toS.call(o)] || (o ? 'object' : 'null'); }
详细测试页面请参考这里:typeof.js
自定义对象惹的祸
来看下面的代码:
function Animal() {} function SubArray() {} SubArray.prototype = []; var toString = Object.prototype.toString; alert(toString(new Animal())); alert(toString(new SubArray())); // firefox: [object Window] // ie: [object Object] // chrome: [object global] alert(new SubArray() instanceof Array); // true alert(new Animal() instanceof Animal); // true
可以看出,Object.toString方法,对于非内置对象来说,在各个浏览器下返回的值各异。
因此,如果要检测非内置对象,还是得用constructor和instaceof. 或者像Mootools一样,通过框架的机制来判断:
Mootools has an interesting approach with it’s $type function. Mootools wraps all native objects via the “Native” constructor in order to easily implement new methods for them. The Native constructor also creates a new property, called $family, which is the value returned by the $type() utility function.
好了,本文结束。JavaScript,每天精进一点点,乐哉!