Skip to content

Instantly share code, notes, and snippets.

@stanleycyang
Created March 24, 2015 15:50
Show Gist options
  • Save stanleycyang/7be1b5b9e4537969cdae to your computer and use it in GitHub Desktop.
Save stanleycyang/7be1b5b9e4537969cdae to your computer and use it in GitHub Desktop.

#Closures & Lexical Scope

###Objectives

  • Understand lexical scope
  • Understand what a closure is
  • Understand the Principle of Least Privilege
  • Be able to create closures

##Lexical Scope

Lexical scoping (sometimes known as static scoping ) is a convention used with many programming languages that sets the scope (range of functionality) of a variable so that it may only be called (referenced) from within the block of code in which it is defined. The scope is determined when the code is compiled. A variable declared in this fashion is sometimes called a private variable.

##Dynamic Scope

The opposite approach is known as dynamic scoping. Dynamic scoping creates variables that can be called from outside the block of code in which they are defined. A variable declared in this fashion is sometimes called a public variable.

Reference: Lexical Scope

###Activity:


###Let's understand lexical scope

***Write this on the board and circle the scope***

function foo(){
	var bar = 10;
	
	function baz(){
		var faz;
		
		function raz(){
			var caz;
		}
		
	}
}

***Circle foo, baz, and raz***	

###Activity Get into groups (count off) and create functions raz > haz > oz. Draw the scoping and present to the class

###What is a closure?

A closure is an inner function that has access to the outer (enclosing) function's variables—scope chain. 

The closure has three scope chains: it has access to: 

1) its own scope (variables defined between its curly brackets), 

2) it has access to the outer function's variables, and 

3) it has access to the global variables.

###Basic closure

// Global scope
var a = 10;

function test(){
  // Outer scope
  var b = 5;

  // This is a closure
  function closure(){
  	// Local scope
  	var c = 1;
  	// Access 
    return [a, b, c];
  }
  closure();
 }
 
 // Run it
 test();

###Activity


**15 Minutes**

Create a function called called addNum. Create a global variable called num1 with a number 10. Inside the addNum function, there is a variable num2 defined as 5. Use a closure called sumNum and inside here is a variable num3. Use the closure to add them up and return the sum.

Get up and explain to someone in class why this is a closure.

###Principle of Least Privilege

You should always use private variables in JavaScript and Ruby. Unless there is a huge need for it, don't use it because it can a) pollute your namespace 2) . The need for closures come from a need to access those variables outside of the scope. For example, if a user entered in an input and it goes into a private variable, they still want to be able to access it. Closures allow that. For example:

function setAnswer(answer){
	var answer = answer;
	function getAdjustedAnswer(){
		answer = answer / 10;
		return answer;
	}
	return getAjustedAnswer();
}

###Activity:

Write down why you want to use a use a closure (2 min) and explain to the person next to you.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment