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?

1
  • 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. Commented Nov 10, 2011 at 22:27

6 Answers 6

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
Sign up to request clarification or add additional context in comments.

2 Comments

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.
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
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

Comments

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.

Comments

5
window.convert = (num1, num2, num3) ->
  num1 + num2 * num3

Comments

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

1 Comment

Think you have a stray space in there: '- >'
0
convert = (num1, num2, num3) -> num1 + num2 * num3

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.