Created
January 11, 2016 15:44
-
-
Save mmloveaa/62cef59b4f98e427a4fd to your computer and use it in GitHub Desktop.
Closure - Alternates
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
// 1/5/2016 | |
// write a function that will return a value that alternates | |
// between a and b | |
// var alternate = createFunc2() | |
// alternate() => 'a' | |
// alternate() => 'b' | |
// alternate() => 'a' | |
// alternate() => 'b' | |
function createFunc() { | |
var value = 'a'; | |
function increment() { | |
if (value==='a') { | |
console.log('a') | |
value='b' | |
} | |
else { | |
console.log('b') | |
value='a' | |
} | |
} | |
return increment | |
} | |
var print= createFunc() | |
print(); | |
print(); | |
print(); | |
print(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment