[英]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.