48

I'm trying to convert a function from Javascript to CoffeeScript. This is the code:

function convert(num1, num2, num3) {
    return num1 + num2 * num3;
}

But how I can do that in CoffeeScript?


I'm trying to run the function from an HTML source like this:

<script type="text/javascript" src="../coffee/convert.js"></script>

<script type="text/javascript">
    convert(6, 3, 10);
</script>

But it won't work and I get an error saying: ReferenceError: Can't find variable: convert

How to correct this?

Nathan Campos
  • 28,769
  • 59
  • 194
  • 300
  • 2
    The answers given are correct, but what you really should do instead is get the remaining JS out of your HTML and into an external JS or CoffeeScript file. JS doesn't belong in HTML. – Marnen Laibow-Koser Nov 10 '11 at 22:27

6 Answers6

78

You need to export the convert function to the global scope.
See How can Coffescript access functions from other assets?

window.convert = (num1, num2, num3) ->
  num1 + num2 * num3
Community
  • 1
  • 1
lawnsea
  • 6,463
  • 1
  • 24
  • 19
  • 5
    This answer is spot-on. It must be emphasized that once attached to `window`, `window.convert` can be accessed from anywhere as just `convert`; but writing `convert =` creates a variable called `convert` with local `var` scope. – Trevor Burnham Jun 01 '11 at 21:13
  • Nice lawnsea, just hit this one today while calling a function from javascript (mixed). I remembered the anonymous scope but forgot Trevor's suggestion to slap it on window to make it callable outside of coffeescript's space – Mark Essel Jun 11 '11 at 21:47
33

@lawnsea answer is great.

I just want to add some thoughts.

Instead of polluting the global namespace, I prefer to add just one variable to the window object.

window.App = {}

Then, you can have access to App globally and add all your stuff there. the function convert can now be expressed this way:

App.convert = convert = (a, b, c) -> a + b * c

Then, to call the function within the local scope

convert 1,2,3

And now globally

App.convert 1,2,3
jaime
  • 41,961
  • 10
  • 82
  • 52
17

At the top level of your coffeescript file, this (aka @) should refer to window. So to attach it here, you could use the shorthand:

@convert = (num1, num2, num3) -> num1 + num2 * num3

Note that this pollutes the global namespace, though. The solution posted by jm- is more prudent. But you can replace

window.App = {}

with

@App = {}

The benefit of using @ is that it refers to global in node.js, so you can use the same code to expose your functions in both browser and serverside environments.

twf
  • 325
  • 4
  • 8
5
window.convert = (num1, num2, num3) ->
  num1 + num2 * num3
edoloughlin
  • 5,821
  • 4
  • 32
  • 61
1

You should check these awesome slides just released today by godfoca http://www.slideshare.net/godfoca/lets-have-a-cup-of-coffeescript Also, you can try code out through-the-web at http://jashkenas.github.com/coffee-script/

convert = (num1, num2, num3) ->
  num1 + num2 * num3
jrhicks
  • 14,759
  • 9
  • 42
  • 57
0
convert = (num1, num2, num3) -> num1 + num2 * num3
Jasmijn
  • 9,370
  • 2
  • 29
  • 43