Skip to content

Instantly share code, notes, and snippets.

@Cheffheid
Created July 22, 2016 01:01
Show Gist options
  • Save Cheffheid/18c765e6ca6c661503cf1cd61640b37d to your computer and use it in GitHub Desktop.
Save Cheffheid/18c765e6ca6c661503cf1cd61640b37d to your computer and use it in GitHub Desktop.
Closure Example
(function () {
var greeter = (function () {
var greeting = "Hey";
var name = "you";
function setName( newName ) {
name = newName;
}
function greet() {
console.log(greeting + " " + name + "!");
}
return {
setName: setName,
greet: greet
}
}());
var john = greeter;
john.greet(); // will log "Hey you!"
john.setName("John");
john.greet(); // will log "Hey John!"
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment