Last active
August 29, 2015 14:10
-
-
Save nekoneko-wanwan/60cef6759d8df621b10c to your computer and use it in GitHub Desktop.
クロージャを使ってカウントアップ処理
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 playFunc(n) { | |
if(n === 1){ | |
console.log('奇数'); | |
} else { | |
console.log('偶数'); | |
} | |
} | |
function counter(){ | |
var count = 0; | |
return function() { | |
if(count === 2 ) { | |
count = 1; | |
} else { | |
count++; | |
} | |
playFunc(count); | |
}; | |
} | |
var num = counter(); | |
setInterval(function(){ | |
num(); | |
}, 1000); | |
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 counter(n) { | |
var cnt = n || 0; | |
return { | |
get: function() { | |
cnt++; | |
return cnt; | |
}, | |
reset: function() { | |
cnt = 0; | |
return cnt; | |
} | |
}; | |
} | |
var countUp = counter(0); | |
console.log(countUp.get()); // 1 | |
console.log(countUp.get()); // 2 | |
console.log(countUp.get()); // 3 | |
console.log(countUp.reset()); // 0 | |
console.log(countUp.get()); // 1 | |
console.log(countUp.get()); // 2 | |
console.log(countUp.get()); // 3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment