Local and global coffeescript coverage

With javascript:

function myFunc() {
    var x = 5;
};

console.log(x);

      

I get //undefined

and with:

function myFunc() {
        x = 5;
    };

    console.log(x);

      

I get 5

With coffeescript

this variable var x = 5;

is equal x = 5

.

For example, is this possible ?:

myFunc ->
  window.x = 5;

    console.log window.x

      

Instead:

myFunc ->
 x = 5;

 console.log x

      

My question is How can I distinguish a global local variable from CoffeeScript?

+3


source to share


1 answer


for global scope you should use functions like this:

myFunc = =>
  @x = 5;

myFunc()

console.log x

      



example of generated code: http://jsfiddle.net/Upward/wZ7w4/

+1


source







All Articles