An in-depth understanding of coffeescript global Variables examples

Source: Internet
Author: User


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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.