This works in the case of 'var' because a stop variable is created in the scope resolution. Without the "var", you simply run away looking through the chains of regions and you are informed . If you really want to use a global variable:
// This is fine because assignment always sets a property value // in this case (no 'var ga') it the same as window.ga = window.ga || [] ga = window.ga || []
Or:
// Once again, only the lookup is affected with "running off" // the lookup chain. It not that the variable has no value
Or even this:
// First line is the same as window.ga = window.ga, // but now the property ga is guaranteed to exist on the window object -- // the VALUE of the property may (still) be undefined ga = window.ga ga = ga || []
Note that in both cases, I explicitly called ga as a property of the window (global) object.
Here you can read the details: Identifier Resolution, Execution Contexts, and Area Chains .
The placement of var within a region does not matter. Everything is the same:
var ga ga = ga || [] var ga = ga || [] ga = ga || [] var ga
user166390
source share