I have the following Coffeescript in test.js
yo () -> console.log("yo")
When compiled via coffee -o public/javascripts/ -cw public/coffeescripts/, I get public/javascripts/test.js:
// Generated by CoffeeScript 1.4.0
(function() {
var yo;
yo = function() {
return console.log('yo');
};
}).call(this);
I’m trying to include this in the usual way in an HTML file:
<script src="/javascripts/test.js" type="text/javascript"></script>
<script type='text/javascript'>
//<![CDATA[
$(function() {
alert('before yo');
yo();
alert('after yo');
});
//]]>
</script>
However I keep getting “Uncaught Reference Error: yo is not defined”. Whats the process for actually using the javascript generated by Coffeescript?
In your CoffeeScript file,
yois a local variable. It is not a global variable. If you want to use that variable from another JavaScript file or from JavaScript in the HTML file, then you will need to makeyoa global variable.You can do this in the CoffeeScript file like so: