CafeHey
CafeHey

Reputation: 5800

Coffeescript - call a function from a string, javascript code does not work

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?

Upvotes: 0

Views: 236

Answers (2)

imazzara
imazzara

Reputation: 430

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

Upvotes: 2

Runner
Runner

Reputation: 254

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.

Upvotes: 0

Related Questions