0

So in javascript I would dynamically call a function from a string like this:

window["function_name"](args);

However if I try this in coffeescript it does not work, is this something to do with the way coffee script does not have a function in the global namespace?

All of the functions are wrapped in a function like this:

(function() {
#code here
}).call(this);

So how do I call a function from a string in coffeescript?

1
  • It depends on where/how the function is defined. You can always write @someFn = () ->... and in another file @['someFn'](). But if your talking about one in the current file, you'll have to make it an object method, either of window as I did or some other object. Commented Aug 4, 2016 at 17:53

2 Answers 2

2

I tried here ("Try Coffescript" section) this (sorry about identation):

  ( ->
    foo = () ->
     alert "it works" ;
  ).call(this); #IFFE on Coffescript

  window["foo"]() ;

And it seems to work

Sign up to request clarification or add additional context in comments.

Comments

0
window["function_name"](args);

This code in CoffeeScript compiles to:

window["function_name"](args);

in javscript.

So the problem is not CoffeeScript, but your code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.