Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>A GOOD way to declare a global variable in Javascript would be <code>window.globalVariableName</code> rather than <code>var globalVariableName;</code> Or if you are not running your application in browser replace window with global object specific to that invironment.</p> <p>The first declaration will work no matter where you are in global scope or in a function. </p> <p>A BAD way to declare a global variable is </p> <pre><code>globalVariableName = 'Foo'; </code></pre> <p>Because it isn't an obvious declaration and looks a lot like just forgetting the var.</p> <p>And finally globalVariables is not a good thing to use. So use them thoughtfully. Try to check if they are not used already by another script say like that</p> <pre><code>if(window.globalVariableName === undefined) window.globalVariableName = 'Bar'; </code></pre> <p>Try reading first chapter of JavaScript Patterns Stoyan Stefanov for more explanation.</p> <p>UPDATE: awsering Andreas Rossberg</p> <p>For example David Flanigan in Definitive Javascript Guide states in 4.6.1 what are global variables, they are properties of the global object. You can confirm this on MDN also. </p> <p>About bad style, i can agree but only partially, because if you don't use the <em>Single var Pattern</em> for variable declarations like this </p> <pre><code>(function(){ var someVar1 = '', someVar2 = null, someVar3 = undefined; })(); </code></pre> <p>your code will look just as bad. </p> <p>As to next version of EcmaScript again you are a little too brief on reference. I've read only that <code>this</code> will be handled differently in strict Javascript. If I understood you correctly this example is what you are referring to </p> <pre><code>function someFunction() { //'use strict'; console.log(this); //Window or undefined console.log(window);// Window or Window } someFunction(); </code></pre> <p>Uncommenting 'use strict'; will give undefined for this in first log, but still Window for the second log.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload