-1

It turns out that there is a similar question, What does “options = options || {}” mean in Javascript?, but that question is general - I am specifically asking about the occurrence of such a statement at the top of a Javascript file. I already understand that if foo is not already defined then it will be initialized with an empty object. Again, my question specifically asks about this usage as the first statement at the top of a Javascript file. Why would one do that? To prevent the object being overwritten by an assignment from a file loaded earlier? In the case I found, there are no files loaded earlier - hence my question:

Question: What is the point of window.foo in the following statement, which is the first statement at the top of a Javascript file (i.e. as opposed to just saying foo = {};)?

foo = window.foo || {};

Note that subsequent lines in the file define properties and methods:

foo.one = 'one';

foo.two = function() {
    return 1+1;
}

They are used as foo.one; and foo.two();

(Follow-up) Question 2: What is the advantage (and the difference in terminology) of the above approach compared to defining those very same things as follows:

var one = 'one';

function two() {
    return 1+1;
}

They are used as one; and two();

Additionally, all of the above are also accessible via window: window.foo.one; and window.one; and window.foo.two(); and window.two();.

(Follow-up) Question 3: Is everything I define at the outermost scope bound to window? And what's the point of that? (i.e. What if it wasn't?)

Community
  • 1
  • 1
user664833
  • 18,397
  • 19
  • 91
  • 140
  • 3
    Well, in the first case you have a controlled unique namespace. In the second case you're polluting the global scope, and you may cause conflicts with other libraries. – elclanrs Jun 04 '14 at 23:09
  • Ah, right. Polluting global scope. – user664833 Jun 04 '14 at 23:10
  • 1
    Why do you post three questions per question? I think you've been here long enough to know better than that. – Lightness Races in Orbit Jun 04 '14 at 23:11
  • 1
    Regarding the duplicate question that everyone so ravenously jumped on, it's difficult to search the web for "window dot something at the top of a Javascript file". I obviously searched Google before asking here. Duh! And I asked a bunch of follow-up questions in one shot because I felt they were related to one another - if I knew more about Javascript, perhaps I could have asked them individually. Are you guys here to help or to feel empowered by down-voting and sneering at exceptional cases such as this? Don't you think my 4k reputation says I know the guidelines around here? Take it easy. – user664833 Jun 04 '14 at 23:40
  • 1
    [\[javascript\] " || {}"](http://stackoverflow.com/search?q=[javascript]+%22+||+{}%22) Your 4k rep certainly says you ought to. – cookie monster Jun 05 '14 at 20:15

1 Answers1

-1

First question: If there is already a foo object, it won't be overwritten.

Second question: If you put things into foo instead of making new symbols, you avoid polluting the namespace.

Third question: Yes. If you run in a browser. That's called the "global scope". It has to go somewhere, right? If you don't want this, you have to make a new scope (via a function), or do namespacing (like with your foo).

Thilo
  • 257,207
  • 101
  • 511
  • 656
  • You shouldn't answer duplicate questions – JK. Jun 04 '14 at 23:13
  • Just trying to help (and not afraid of the downvotes, I have been around long enough to remember when Stackoverflow was a friendlier place) – Thilo Jun 04 '14 at 23:14
  • Stop crying. Your first two answers are poor. If you're going to answer easy, basic, oft-asked questions, at least put effort in to getting it right. – cookie monster Jun 04 '14 at 23:32
  • Thanks for your attempt to answer my questions. It's unfortunate that some folks were so quick to down-vote and get negative. – user664833 Jun 04 '14 at 23:42
  • @cookiemonster: feel free to post your own answer or edit mine. I actually think my third answer is a bit poor. What's wrong with the first two? – Thilo Jun 05 '14 at 01:10
  • If `foo` is any "falsey" value it will be overwritten. What are "new symbols"? What does "globbering" mean? What "namespace" are you referring to? Did you mean the global variable scope? Why would I post an answer to such a common duplicate? – cookie monster Jun 05 '14 at 20:13