Skip to content

Latest commit

 

History

History
69 lines (50 loc) · 1.34 KB

globals.md

File metadata and controls

69 lines (50 loc) · 1.34 KB

Global Variables

This example is to illustrate:

  1. the different ways to call globals,
  2. the ways in which globals can be inadvertantly created

how to access global variables

globalvar = "heyimaglobal";
console.log(globalvar); // 'heyimaglobal'
console.log(window.globalvar); // 'heyimaglobal'
console.log(window["globalvar"]); // 'heyimaglobal'
console.log(this.globalvar); // 'heyimaglobal'

global anti-patterns (with correct patterns)

BAD

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??

GOOD

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

BAD

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'

GOOD

function example() {
	var a, b;
	a = b = 9;
}

example();

console.log(window.a); // 'undefined'
console.log(window.b); // 'undefined'