Created
May 15, 2015 15:49
-
-
Save leshy/1e10fe30efea43ce72ea to your computer and use it in GitHub Desktop.
This file contains 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
# 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