Skip to content

Instantly share code, notes, and snippets.

@leshy
Created May 15, 2015 15:49
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 leshy/1e10fe30efea43ce72ea to your computer and use it in GitHub Desktop.
Save leshy/1e10fe30efea43ce72ea to your computer and use it in GitHub Desktop.
# a trick that makes class instances function instances and not object instances
# aka - callable objects. (expects object to have a 'call' function defined)
callable = (cls) ->
callable_cls = (...args) ->
obj = (...args) -> obj.call.apply obj, args
obj.__proto__ = cls::
cls.apply obj, args
obj
# usage:
class1 = ->
@bla = 666
class1::f = -> 3
class1::call = -> @bla
class2 = callable class1
obj = new class2()
console.log obj() # tada
console.log obj.f()
console.log obj.bla
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment