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.
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.
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
The statement x = a = {}
means that {}
is assigned to a
that is assigned to x
. So it’s equal to a = {}; x = a
.
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.
This is equivalent to:
jQuery.prototype = {}
jQuery.fn = jQuery.prototype
In other words jQuery.fn and jQuery.prototype both point to the same object.