Last active
August 29, 2015 13:57
-
-
Save moutend/9520709 to your computer and use it in GitHub Desktop.
CoffeeScript closure
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
I = ( name ) -> | |
task = 0 | |
obj = | |
hasTask: -> | |
return "#{ name } have #{ task } task(s)" | |
getTask: -> | |
return ++task | |
return obj | |
me = I 'I' | |
you = I 'You' | |
console.log me.getTask() | |
console.log me.getTask() | |
console.log me.getTask() | |
console.log me.hasTask() | |
console.log you.hasTask() | |
# // Generated by CoffeeScript 1.7.1 | |
# (function() { | |
# var I, me, you; | |
# | |
# I = function(name) { | |
# var obj, task; | |
# task = 0; | |
# obj = { | |
# hasTask: function() { | |
# return "" + name + " have " + task + " task(s)"; | |
# }, | |
# getTask: function() { | |
# return ++task; | |
# } | |
# }; | |
# return obj; | |
# }; | |
# | |
# me = I('I'); | |
# you = I('You'); | |
# | |
# console.log(me.getTask()); | |
# console.log(me.getTask()); | |
# console.log(me.getTask()); | |
# console.log(me.hasTask()); | |
# console.log(you.hasTask()); | |
# | |
# }).call(this); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment