Skip to content

Instantly share code, notes, and snippets.

@moutend
Last active August 29, 2015 13:57
Show Gist options
  • Save moutend/9520709 to your computer and use it in GitHub Desktop.
Save moutend/9520709 to your computer and use it in GitHub Desktop.
CoffeeScript closure
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