Skip to content

Instantly share code, notes, and snippets.

@leshy
Created May 15, 2015
Embed
What would you like to do?
# 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