Playing a little with coffeescript and Rails 3.1.0.rc4. Have this code:
yourMom = (location) ->
console.log location
yourMom "wuz hur"
When the page loads, this outputs "wuz hur" properly. But when I try to call
yourMom("wuz hur")
from the chrome js console (as I do sometimes to test normal JS functions), I get a "ReferenceError: yourMom is not defined"
Are functions generated by coffeescript available in this way?
You can simply invoke a function by placing parenthesis after its name as shown in the following example. // Generated by CoffeeScript 1.10. 0 (function() { var add; add = function() { var a, b, c; a = 20; b = 30; c = a + b; return console. log("Sum of the two numbers is: " + c); }; add(); }).
Easy Maintenance and Readability: It becomes easy to maintain programs written in CoffeeScript.It provides the concept of aliases for mostly operators that makes the code more readable.
You simple need to add a <script type="text/coffeescript" src="app. coffee"></script> to execute coffee script code in an HTML file. In other cases, I've seen people use the attributes of type="coffeescript" and type="coffee" , so they might work for you as well. Save this answer.
In ES6, JavaScript also added many powerful new features and also had a larger community. As of today, January 2021, CoffeeScript is almost dead on the market.
an easier way to share global methods/variables is to use @ which means this.
@yourMom = (location) ->
console.log location
yourMom "wuz hur"
Nicer syntax and easier to read, but I don't encourage you to create global methods/variables
This happens because coffeescript wraps everything in a closure. The JavaScript output of that code is actually:
(function() {
var yourMom;
yourMom = function(location) {
return console.log(location);
};
yourMom("wuz hur");
}).call(this);
If you want to export it to the global scope, you can either do:
window.yourMom = yourMom = (location) ->
console.log location
or
this.yourMom = yourMom = (location) ->
console.log location
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With