什么使Firebug / Chrome控制台将自定义对象视为数组? [英] What makes Firebug/Chrome console treat a custom object as an array?

查看:204
本文介绍了什么使Firebug / Chrome控制台将自定义对象视为数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在jQuery中开发时,我经常发现自己在Chrome / Firebug控制台中输入选择器并查看他们给我的东西。它们总是很好地格式化,就好像它们是数组一样:



我正在试图弄清楚它是什么让控制台将一个对象当作一个数组来处理。例如,下面的自定义对象不被视为数组:

  function ElementWrapper(id){
this [ 0] = document.getElementById(id);
}



如果我然后添加长度 property和一个 splice 方法,它奇迹般地作为一个数组使用,整数键的任何属性都被视为数组的成员:

 函数ElementWrapper(id){
this [0] = document.getElementById(id);
this.length = 1;
this.splice = Array.prototype.splice;
}

>



所以基本上我的问题是:是什么决定了控制台将对象显示为数组?是否有任何理由,或者它是完全武断的如果一个对象具有这些属性,它必须是一个数组?如果是这样,什么是决定性的属性?

解决方案

这是Firebug的 isArray 方法可以:(来自 Firebug来源

  if(!obj)
return false;
else if(is&&!isFunction(obj)&& typeof obj ==object&& isFinite(obj.length)&& obj.nodeType!= 8)
返回true;
else if(isFinite(obj.length)&& isFunction(obj.splice))
return true;
else if(isFinite(obj.length)&& isFunction(obj.callee))//参数
返回true;
else if(instanceOf(obj,HTMLCollection))
return true;
else if(instanceOf(obj,NodeList))
return true;
else
返回false;

当然,这些检查都不能确保该对象是一个真正的JavaScript数组,但它们会执行猜测一个对象是否是一个伪数组,这反过来又给你一个方便的类似于数组的表示以进行调试。



Chrome可能会也可能不会使用这些检查,并且Firefox 4中的新Web控制台不能识别除真数组之外的其他任何数据。


When I am developing in jQuery, I frequently find myself typing selectors into the Chrome/Firebug console and seeing what they give me. They are always nicely formatted as if they were arrays:

I am trying to work out what it is that makes the console treat an object as an array. For instance, the following custom object is not treated as an array:

function ElementWrapper(id) {
    this[0] = document.getElementById(id);
}

If I then add a length property and a splice method, it magically works as an array, with any properties with integer keys treated as members of the arrays:

function ElementWrapper(id) {
    this[0] = document.getElementById(id);
    this.length = 1;
    this.splice = Array.prototype.splice;
}

So essentially my question is: what determines whether the console displays an object as an array? Is there any rationale to it, or is it a completely arbitrary "if an object has these properties, it must be an array?" If so, what are the decisive properties?

解决方案

This is what Firebug's isArray method does: (from the Firebug source)

if (!obj)
    return false;
else if (isIE && !isFunction(obj) && typeof obj == "object" && isFinite(obj.length) && obj.nodeType != 8)
    return true;
else if (isFinite(obj.length) && isFunction(obj.splice))
    return true;
else if (isFinite(obj.length) && isFunction(obj.callee)) // arguments
    return true;
else if (instanceOf(obj, "HTMLCollection"))
    return true;
else if (instanceOf(obj, "NodeList"))
    return true;
else
    return false;

Of course, none of these checks ensures that the object is a true JavaScript array, but they do a reasonable job of guessing whether an object is a pseudo-array, which in turn gives you a convenient array-like representation for debugging.

Chrome may or may not use these same checks, and the new Web Console in Firefox 4 doesn't recognize anything other than true arrays as arrays.

这篇关于什么使Firebug / Chrome控制台将自定义对象视为数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆