Example
A = 5
b =->
Return 5
Compiled JavaScript code:
(function () {
var a, B;
A = 5;
b = function () {
return 5;
};
). Call (this); <-defines an anonymous function and passes in the ' this ' context and executes, a, B are local variables
But we may have the need to define global variables, so Coffeescript still provides a declarative way to not exterminate global variables:
If you are like "to" create top-level variables for "Other scripts", attach them as properties on Windows, or on the Expor TS object in Commonjs. The existential operator (covered below), gives your a reliable way out where to add figure; If you ' re targeting both COMMONJS and the browser:exports? This
In a browser environment, you can define this:
Window.foo = ' Baz '
In the Node.js environment, you can define this:
Exports.foo = ' Baz '
To enhance the robustness of the code, both ends are applicable, you can write this:
root = exports? This
Root.foo = ' Baz '
There is also a more dirty writing, is a pair of ' symbols wrapped code:
' foo = ' Baz '
Compiled code:
(function () {
foo = ' Baz '; <-Foo is a global variable
). Call (this);
But this is what we should try to avoid, because even Var is not added.