是什么让 jQuery 对象在 Chrome 的开发人员工具中显示为数组? [英] What makes a jQuery object show up as an array in Chrome's Developer Tools?

查看:23
本文介绍了是什么让 jQuery 对象在 Chrome 的开发人员工具中显示为数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道 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>​]

但下列说法是错误的:

var a = $("<a>");

Array.isArray(a);   // false
a instanceof Array; // false

我尝试修改 jQuery 并查看会发生什么,令人惊讶的一件事是从 jQuery 函数中删除 length 会删除数组符号:

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 代码中是什么让开发人员工具将实例显示为数组.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()).

基本上,对象必须具有 lengthsplice 属性才能像数组一样.这是一个相关的 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屋!

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