Coffeescript equivalent to Python's getattr
In python I can put a function into a variable at runtime, then call it using the getattr function
method = getattr(self,self.name)
method()
Is there a similar way to do this in 开发者_Go百科Coffeescript? Thanks!
Using zeekay suggestion but using classes would be:
class Test
foo: -> alert 'foo'
foo2: methodName -> this[methodName]()
x = new Test
x.foo2('foo')
In Javascript objects are associative arrays, and you can access property/methods using the name of the property as the key:
obj =
method: -> 'xxx'
method = obj['method']
method() # 'xxx'
Your updated example doesn't work because foo2
is merely returning foo
. You might want to try this:
class Test
foo: -> alert 'foo'
foo2: -> this['foo']() # or @['foo']()
x = new Test
x.foo2()
Objects and classes in CoffeeScript work very differently from how they work in Python. This can be frustrating, but the aim of CoffeeScript is to stay as close to JavaScript as possible, rather than adding a thick layer of abstraction. So, a few things to keep in mind:
- As zeekay answered,
x[y]
is equivalent togetattr
/setattr
(except that it returnsundefined
ifx
has no property with keyy
, rather than throwing an exception); - Functions you attach to a class using the syntax
foo: -> ...
are just methods of the class' prototype, and can be modified at any time (or overridden on a particular instance). - After running
method = x.method
, there's one crucial difference betweenx.method()
andmethod()
, even though both run the same function: When you run the function asx.method()
,this
points tox
; but when you run it asmethod()
,this
points to the global object (window
in the browser,global
in Node.js).
Check out my book, CoffeeScript: Accelerated JavaScript Development, if you'd like to learn more. :)
Your foo2
returns the foo
method intead of calling it. Change
return this['foo'];
to:
this['foo']();
It looks like this is a yes, but you have to use the fat arrow operator in the method definition:
class O
method2 =>
console.log(this)
If I understand this correctly, that syntax tells coffeescript to bind the instance to the method, allowing it to be called from outside the instance. If you use the thin arrow, coffeescript does not keep track of binding information, resulting in Javascript pointing this
at something unexpected if you don't call the method via the instance.
class O
method2 ->
console.log(this)
class Test
foo: -> alert 'foo'
foo2: -> this['foo']
When you call x.foo2()
it's not doing what you think it's doing.
-> this['foo']
This function returns the value of this['foo']
which is a function object. So the return value is a function object. It doesn't run the function.
Try this:
class Test
foo: -> alert 'foo'
foo2: -> this['foo']()
Now when call foo2()
it get the funciton object and executes it.
Also though, you only need the bracket notation when using a dynamic string as an accessor. This is the exactly the same thing:
class Test
foo: -> alert 'foo'
foo2: -> @foo()
精彩评论