是什么让 jQuery 对象在 Chrome 开发工具中显示为数组?
我想知道 jQuery 对象如何在 Chrome 开发者工具的控制台日志中显示为数组。
例如,如果我执行 $('')
,我在控制台日志中看到的是:
[<a></a>]
但以下语句是错误的:
var a = $("<a>");
Array.isArray(a); // false
a instanceof Array; // false
我尝试修改 jQuery 并查看会发生什么,并且有一件事令人惊讶的是,从 jQuery 函数中删除 length
会删除数组符号:
length: 0, // commenting this line removes array notation
相反,它会显示为(箭头是要展开的实心箭头):
> jQuery.jQuery.fn.jQuery.init
但是,如果我尝试创建自己的构造函数它应该以数组表示法显示,它不起作用:
var test = function() { this.length = 0 };
new test();
// Logged (arrow is same one as before):
// > test
所以我想知道 jQuery 代码中的什么使开发人员工具将实例显示为数组。 jQuery 中添加了哪些属性/函数/事物,使得开发人员工具在显示实例时将其作为数组处理?
I'm wondering how it's possible that jQuery objects show up as an array in the console log of Developer Tools in Chrome.
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
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
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
来自 http://api.jquery.com/jQuery.makeArray/:
基本上,对象必须具有
length
和splice
属性才能类似于数组。这是一个相关的问题: Javascript 中的类似数组的对象From http://api.jquery.com/jQuery.makeArray/:
Basically, The object has to have
length
andsplice
properties to be array-like. Here is a relevant SO question: Array Like Objects in Javascript您可能知道这一点,但是
console.log
并没有“按原样”显示传递的内容,它试图变得“智能”并进行一些后期处理。如果您想“按原样”查看原始对象,可以使用console.dir
方法。You probably know this, but
console.log
is not displaying passed content "as is", it is trying to be "smart" and does some post processing. If you want to see original object "as is", there isconsole.dir
method.