funName = () ->
$(".foo").addClass("bar");
Compiles into the scope of an anonymous function. Calling funName from the console results in undefined.
(function() {
var funName;
funName = function() {
return $(".foo").addClass("bar");
};
}).call(this);
What’s its reasoning for compiling like this and how do I work with it?
Also any insight on the mandatory return within functions using CoffeeScript would be great. Why is it like that? How do I need to code differently because of it?
Mike has answered the main question here. The modular wrapper a common point of confusion for CoffeeScript newcomers, as illustrated by these related questions:
As to your other question: If you don’t want a function to return anything, simply make the last line of that function either
returnby itself or, equivalently,undefined. Either will compile to a function with noreturn. For instance:compiles to
Note that there is an ongoing discussion (issue 899) about a possible alternative syntax for defining no-return functions. If the current proposal were accepted, you’d be able to write your function as
If you like that syntax, you should voice your support for it.