This example is to illustrate:
- the different ways to call globals,
- the ways in which globals can be inadvertantly created
globalvar = "heyimaglobal";
console.log(globalvar); // 'heyimaglobal'
console.log(window.globalvar); // 'heyimaglobal'
console.log(window["globalvar"]); // 'heyimaglobal'
console.log(this.globalvar); // 'heyimaglobal'
function multiply(x, y) {
this_is_a_global_result = x * y;
return this_is_a_global_result;
}
multiply(3,4);
console.log(window.this_is_a_global_result); //what? how'd result get into the global scope??
function multiply(x, y) {
var not_a_global_result = x * y; //notice that 'var' has been added
return not_a_global_result;
}
multiply(3,4);
console.log(window.not_a_global_result); //undefined - which is good, means that our var didn't leak into global scope
function example() {
var a = b = 9; //right-to-left evaluation makes this as if var a = (b = 0) was written
}
example();
console.log(window.a); // 'undefined'
console.log(window.b); // '9'
function example() {
var a, b;
a = b = 9;
}
example();
console.log(window.a); // 'undefined'
console.log(window.b); // 'undefined'