Created
July 16, 2014 12:05
-
-
Save marshluca/7b0d331f77faf3d7a64d to your computer and use it in GitHub Desktop.
define dynamic methods in CoffeeScript
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
class MyClass | |
constructor: (@name) -> | |
for k, v of ['get', 'set'] | |
console.log('creating method: ' + v) | |
MyClass::[v] = (args...) -> | |
method = v | |
console.log('executing method: ' + method) | |
o = new MyClass('dummy') | |
o.get() | |
o.set() | |
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
(args...) -> | |
method = v | |
console.log('executing method: ' + method) | |
build_method = (v) -> | |
(args...) -> | |
method = v | |
console.log('executing method: ' + method) | |
for k, v of ['get', 'set'] | |
console.log('creating method: ' + v) | |
MyClass::[v] = build_method(v) | |
for k, v of ['get', 'set'] | |
do (k, v) -> | |
console.log('creating method: ' + v) | |
MyClass::[v] = (args...) -> | |
method = v | |
console.log('executing method: ' + method) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment