简体   繁体   English

jQuery的返回值是Array,但构造函数是Object。 怎么样?

[英]jQuery's return value is Array, but constructor is Object. how?

for example, 例如,

$(document) // [ #document ] : document object in array
typeof $(document) // "object"
$(document).constructor // function Object() { [native code] } or function (a,b) { return some function; }

If value is array, it have to be Array constructor. 如果value是数组,则必须是Array构造函数。

It's not an array like object. 它不是像对象一样的数组。 because array like object just has array property, not like []. 因为像对象这样的数组只有数组属性,而不像[]。

How can it be? 怎么可能?

add: If you can, show simple example code, please. 添加:如果可以,请显示简单的示例代码。 like 喜欢

a = ... 
console.log(a) // [ ... ]
console.log(a.constructor) // function Object or something

Take a look at the jQuery source . 看一下jQuery 源代码 $(document) creates a cloned jQuery object of the document element and then makes and returns an array-like object . $(document)创建文档元素的克隆jQuery对象,然后生成并返回类似数组的对象

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()). jQuery工厂函数$()返回一个jQuery对象,该对象具有数组的许多属性(长度,[]数组访问运算符等),但与数组不完全相同,缺少一些数组的内置方法(如.pop()和.reverse())。

An array, as in [] , is also an object in JavaScript. []的数组也是JavaScript中的对象。 In JS mostly everything is an object, such as functions, arrays, literal objects, regular expressions... To really know what you're dealing with is an array you can do this: 在JS中,大多数都是一个对象,例如函数,数组,文字对象,正则表达式......要真正知道你正在处理的是一个数组,你可以这样做:

Object.prototype.toString.apply(myarray) === '[object Array]'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何定位数组 object 中的 boolean 值。 JavaScript - How to target boolean value in array object. JavaScript jQuery错误TypeError:值不是对象。 与.split(',') - jquery error TypeError: Value not an object. with .split(',') 我的 function 返回 object。 如何在 React 中将返回值设置为特定的键值 - My function returns an object. How to set return value to specific key value in React 我想正确返回儿童的 object。 我怎样才能? - i wanna return correctly children's object. how can i? 如何 map 对象数组并返回每个对象值的长度? - How to map Array of Objects and return the length of each Object's value? 如何将数组值作为对象键传递,并将该键与对象中的某些值相关联。 然后将其传递给列表项 - how to pass an array value as an object key, and associate that key with some values in the object. and then pass it into a list item JavaScript和jQuery,创建和使用对象。 属性返回未定义 - JavaScript and jQuery , creating and usng an object. Properties return undefined 如何使用js或jQuery在数组中选择当前对象的值 - How to select a current object's value in array with js or jQuery 下划线:平面对象的对象数组。 什么是魔术? - Underscore: Array of objects to flat object. What's the magic? 将数组发送到JQuery中的对象构造函数 - Send an Array to an object constructor in JQuery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM