Skip to content

Instantly share code, notes, and snippets.

@jampekka
Last active August 29, 2015 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jampekka/64977103d193f6252f2c to your computer and use it in GitHub Desktop.
Save jampekka/64977103d193f6252f2c to your computer and use it in GitHub Desktop.
Classhack
# Motivation for this madness: I often find that simply a function is
# a nice interface objects. However, often later on a need arises that
# we need also something else in a return value / argument, which currently
# calls for refactoring of every call site. In Python this can be hacked
# around using __call__. This would be a way to hack around it in LiveScript.
#
# And anyway I think having separate "classes" and "objects" is a bit stupid
# historical artefact. KISS!
fobj = (f) -> (...args) ->
me = {}
callable = f.apply(me, args) ? me
# Would maybe be nicer using proto
for key, value of me
callable[key] = value
return callable
AFunctionObject = fobj (@member_arg, private_arg, @other_arg='default') ->
@someOtherStuff = 'whatever'
@printArg = ~>
console.log @member_arg
# The "default function"
->
console.log "I'm just a function!"
func = AFunctionObject 'foo', 'bar
func!
func.printArg!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment