Created
August 15, 2012 22:08
-
-
Save EvanHahn/3364133 to your computer and use it in GitHub Desktop.
Getting private member functions working in CoffeeScript, with .call()
This file contains hidden or 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
# This is an example in a post about private members in CoffeeScript. | |
# Read more: http://evanhahn.com/?p=1126 | |
class Sorcerer | |
constructor: (@spell) -> | |
conjureSpell = -> # private | |
@spell.conjure() | |
useSpell: -> | |
conjureSpell.call(this) # Note this little hack | |
@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