是什么让一个jQuery对象显示为在Chrome的开发者工具数组? [英] What makes a jQuery object show up as an array in Chrome's Developer Tools?
问题描述
我不知道它是如何可能的jQuery对象显示为开发者工具的Chrome浏览器的控制台日志数组。
I'm wondering how it's possible that jQuery objects show up as an array in the console log of Developer Tools in Chrome.
例如。如果我执行 $('< A>')
,我在控制台日志中看到的是:
E.g. if I execute $('<a>')
, what I see in the console log is:
[<a></a>]
但下面的语句是错误的:
But the following statements are false:
var a = $("<a>");
Array.isArray(a); // false
a instanceof Array; // false
我试图修改jQuery和看看会发生什么,一件事,这是令人惊讶的是,删除长度
从jQuery函数删除数组表示法:
I tried to modify jQuery and see what happens, and one thing that was surprising is that removing length
from the jQuery function removes the array notation:
length: 0, // commenting this line removes array notation
相反,它会显示为(箭头是坚实的展开):
Instead, it then shows up as (arrow is that solid one to expand):
> jQuery.jQuery.fn.jQuery.init
但是,如果我尝试让我自己构造这应该显示在数组符号,这是行不通的:
But, if I try to make my own constructor which is supposed to be displayed in array notation, it does not work:
var test = function() { this.length = 0 };
new test();
// Logged (arrow is same one as before):
// > test
所以我想知道什么在jQuery的code使得开发人员工具显示实例作为数组。什么属性/功能/东西被添加到jQuery的,使开发者工具处理它作为数组显示实例时?
So I'm wondering what in the jQuery code makes Developer Tools show instances as an array. What property/function/thing is added to jQuery that makes Developer Tools handle it as an array when displaying an instance?
推荐答案
http://api.jquery.com/jQuery .makeArray / :
许多方法,无论是在jQuery和JavaScript中一般,返回为数组状物体。例如,jQuery的工厂函数$()返回一个有许多数组(长度,则[]数组访问运算符等)的性质的jQuery对象,但不完全一样的一个数组,缺少一些的阵列的内置方法(如.pop()和.reverse())。
Many methods, both in jQuery and in JavaScript in general, return objects that are array-like. For example, the jQuery factory function $() returns a jQuery object that has many of the properties of an array (a length, the [] array access operator, etc.), but is not exactly the same as an array and lacks some of an array's built-in methods (such as .pop() and .reverse()).
基本上,对象必须具有长度
和拼接
属性中,将阵列等。这里有一个相关的SO问题:阵列像在Javascript对象
Basically, The object has to have length
and splice
properties to be array-like. Here is a relevant SO question: Array Like Objects in Javascript
这篇关于是什么让一个jQuery对象显示为在Chrome的开发者工具数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!