Skip to content

Instantly share code, notes, and snippets.

@EvanHahn
Created August 15, 2012 22:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EvanHahn/3364153 to your computer and use it in GitHub Desktop.
Save EvanHahn/3364153 to your computer and use it in GitHub Desktop.
Getting private member functions working in CoffeeScript, by scoping differently
# This is an example in a post about private members in CoffeeScript.
# Read more: http://evanhahn.com/?p=1126
class Sorcerer
that = null
constructor: (@spell) ->
that = this
conjureSpell = -> # private
that.spell.conjure() # Note this hack
useSpell: ->
conjureSpell()
@spell.use()
s = new Sorcerer
conjure: -> console.log "Brewing potion..."
use: -> console.log "Now I have superpowers!"
s.useSpell()
# This code by Evan Hahn (evanhahn.com) is licensed under the Unlicense.
# http://unlicense.org/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment