现在的位置: 首页 > 综合 > 正文

JavaScript类型检测小结(下)

2018年05月15日 ⁄ 综合 ⁄ 共 2701字 ⁄ 字号 评论关闭
文章目录

回归简单: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,每天精进一点点,乐哉!

参考资料

  1. 文中提到的犀牛书指的是:OReilly.JavaScript.The.Definitive.Guide.5th.Edition.Aug.2006
  2. How to write a robust ‘isArray’(很详细的介绍,一步一步,很有条理)
  3. The Miller Device(老道的文章总是言简意赅,一针见血……)
  4. getClass.js(toString方法检测Class的一个实现)
  5. typeof增加版(很不错的实现方案)
  6. toString in MDC(Mozilla是与时俱进的官方)

Tags: ,

RSS feed | Trackback URI
6 comments
  1. 1.

    lifesinger said:
    February 7th, 2009 on 9:26

    winter:

    这个其实跟浏览器类型检查是一个道理 对象类型检查不如对象属性检查
    鸭子类型的本意其实不是prototype那样用的 跟下面这句话一个道理:
    小心伪特性探测:比如if(window.ActiveXObject) { // 一大堆针对ie但和ActiveXObject没任何关系的代码 }

    还有就是
    typeof检查type
    Object.toString检查私有属性[[class]]
    constructor检查公有constructor
    instanceof实际上检查的是私有属性[[prototype]]

    这些东西你说它是类型确实挺像 但是其实混到一起也没什么价值

  2. 2.
    lifesinger said:
    February 7th, 2009 on 9:26

    hax:

    1.xxx 不行,不是因为 1 上面没有 xxx,而是语法不合。写成(1).xxx就可以了。也就是说number和string其实都是有constructor属性的。当然这实际隐含了primitive到object的自动转换。

  3. 3.
    lifesinger said:
    February 10th, 2009 on 10:01

    hax: try

    Object.prototype.toString.call(null)
    Object.prototype.toString.call(document.all)
    Object.prototype.toString.call(document.getElementById)
    Object.prototype.toString.call(document.all.item)
    

    最神奇的当属item了

抱歉!评论已关闭.