Skip to content Skip to sidebar Skip to footer

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 understa

Solution 1:

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

Solution 2:

This is equivalent to:

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

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

Solution 3:

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

Solution 4:

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.

Post a Comment for "Javascript Variables, What Does Var X = A = {} Do?"