4

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.

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
4
  • I don't understand your reasoning for why the return from $(document) is "not an array like object" - if you look at the actual jQuery source you'll find it is an array-like object and not an array... Commented Jul 9, 2012 at 6:07
  • @nnnnnn because array-like object is not shown as [] Commented Jul 9, 2012 at 6:15
  • "Shown" where? The return from $(document) is an instance of jQuery.fn.init which has had 0 and length properties added. If it was an Array it would have array methods like .join() and it doesn't. Look at the source code. Commented Jul 9, 2012 at 6:20
  • Thanks your answer. Actually I confused about concept of return instance. Commented Jul 9, 2012 at 6:30

2 Answers 2

2

Take a look at the jQuery source. $(document) creates a cloned jQuery object of the document element and then makes and returns an array-like object.

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()).

Sign up to request clarification or add additional context in comments.

4 Comments

Now I am confused. If it is returning an array of objects shouldn't its constructor give function Array() {//..}?
It doesn't make or return an array, it returns an instance of jQuery.fn.init after adding 0 and length properties. That is, it returns an array-like object.
Right, got it! The output is an object that have some properties of an array, but it is not an array.
Note: in the case of $(document) (and indeed most other cases) the $() function returns before it gets to the last line that returns the result of jQuery.makeArray(). The array access operator [] isn't really "array access" because it applies to all JS objects.
0

An array, as in [], is also an object in 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:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.