Vikram
Vikram

Reputation: 4192

What does jQuery(empty object literal) really do?

I came across this article where I encountered the following line:

var o = jQuery({});

If did typeof o it says "object". However, this object when consoled shows:

Object[Object {}]

Can anyone try to explain what is going on here?

Thanks for your time

Upvotes: 1

Views: 52

Answers (1)

Pointy
Pointy

Reputation: 413895

It just wraps a new empty object in a jQuery object. All jQuery objects have a length property, so they kind-of look like arrays. The library also keeps the elements or objects wrapped as properties with numeric indexes, so again it kind-of looks like an array.

So, after jQuery({}), you've got an object with a property called "0", and the value of that property is your new empty object. The wrapper object also has a "length" property, whose value is 1.

The jQuery library actually has fairly extensive support for wrapping its facilities around plain objects. That means you can attach event handlers to plain objects and trigger them, and other fun things like that.

Upvotes: 3

Related Questions