Created
July 22, 2016 01:01
-
-
Save Cheffheid/18c765e6ca6c661503cf1cd61640b37d to your computer and use it in GitHub Desktop.
Closure Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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