Skip to content

Instantly share code, notes, and snippets.

@marshluca
Created July 16, 2014 12:05
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 marshluca/7b0d331f77faf3d7a64d to your computer and use it in GitHub Desktop.
Save marshluca/7b0d331f77faf3d7a64d to your computer and use it in GitHub Desktop.
define dynamic methods in CoffeeScript
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()
(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