bob
bob

Reputation:

javascript variables, What does var x = a = {} do?

I see in jQuery something like this:

jQuery.fn = jQuery.prototype = {}

Why is this being done? Isn't this the same thing just saying jQuery.prototype = {}? I'm not sure I understand what Resig is accomplishing here.

Upvotes: 2

Views: 1255

Answers (4)

FryGuy
FryGuy

Reputation: 8744

One thing to know is that in javascript, every expression has a return value, regardless of if it has any side effects (of assignment)

From right to left, you have the following statements:

(jQuery.fn = (jQuery.prototype = ({})))

Evaluating the first part gives an empty object: {}:

(jQuery.fn = (jQuery.prototype = {}))

The second statement executes and sets jQuery.prototype to {}, and it evaluates to jQuery.prototype, giving the second part:

(jQuery.fn = jQuery.prototype)

which sets jQuery.fn to jQuery.prototype, which then evaluates to:

jQuery.fn

which does nothing.

Upvotes: 2

victor hugo
victor hugo

Reputation: 35848

The same as:

jQuery.prototype = {}
jQuery.fn = jQuery.prototype

In my opinion having all in one line makes more clear that your assigning the same value to both variables

Upvotes: 13

Kevin Loney
Kevin Loney

Reputation: 7553

This is equivalent to:

jQuery.prototype = {}
jQuery.fn = jQuery.prototype

In other words jQuery.fn and jQuery.prototype both point to the same object.

Upvotes: 3

Gumbo
Gumbo

Reputation: 655509

The statement x = a = {} means that {} is assigned to a that is assigned to x. So it’s equal to a = {}; x = a.

Upvotes: 2

Related Questions