Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I was reading Ben Cherry's "JavaScript Module Pattern: In-Depth", and he had some example code that I didn't quite understand. Under the Cross-File Private State heading, there is some example code that has the following:

var _private = my._private = my._private || {}

This doesn't seem to be different from writing something like this:

var _private = my._private || {}

What's happening here and how are these two declarations different?

share|improve this question

closed as off topic by FrustratedWithFormsDesigner, Thomas Owens, unholysampler, Mark Trapp Dec 3 '11 at 16:07

Questions on Programmers Stack Exchange are expected to relate to software development within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

1  
I've locked this question to prevent it from being migrated a second time: obviously there's a confusion about what's on-topic here. I've opened a question on our meta discussion site to discuss whether this question is on-topic here. –  user8 Nov 29 '11 at 19:31
    
Even though this question got more action here than on Stack Overflow, based on community consensus, I have to close this question. –  user8 Dec 3 '11 at 16:07
add comment

1 Answer

up vote 11 down vote accepted
var _private = my._private = my._private || {}

This line means use my._private if it exists, otherwise create a new object and set it to my._private.

More than one assignment expression can be used in a statement. The assignment operator uses (consumes) whatever is to the right of it and produces that value as its output to the left of the variable being assigned. So, in this case, with parentheses for clarity, the above is equivalent to var _private = (my._private = (my._private || {}))

This case is a type of lazy initialization. A less terse version would be:

if (!my._private) {
    my._private = {};
}
var _private = my._private;

In this case, it seems that the lazy initialization is more used for anywhere initialization than laziness. In other words, all functions can include this line to safely create or use my._private without blowing away the existing var.

share|improve this answer
    
Correct, but there are two layers of this. Not only does my._private get set to my._private || {}, but _private gets set to the same value. –  Ryan Kinal Nov 29 '11 at 19:22
    
ah! Thanks Ryan. I understood the bit about my._private || {} what I didn't get was the _private = my._private = my._private || {}. So the expression after the last assignment operator is the value, the names to the left of that assignment operator are getting assigned the expression? –  JoeM05 Nov 29 '11 at 19:41
2  
@JoeM05 the assignment operator uses (consumes) whatever is to the right of it and produces that value as it's output to the left of the variable being assigned. So, with parentheses for clarity, the above is equivalent to var _private = (my._private = (my._private || {})) –  NickC Nov 29 '11 at 20:02
    
@Renesis , Thanks. That answers my question. –  JoeM05 Nov 29 '11 at 21:11
    
@Renesis, Your comment does a better job of answering my question then the actual answer does. Should I accept the answer anyway? It sort of round-about-ly lead me to understanding but then your above comment made it crystal clear. –  JoeM05 Nov 30 '11 at 1:48
show 1 more comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.