Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

local and global scope coffeescript [duplicate]

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 x = 5.

For example this is possible?:

myFunc ->
  window.x = 5;

    console.log window.x

Instead of:

myFunc ->
 x = 5;

 console.log x

My question is How I can differentiate a global variable of a local variable with CoffeeScript?

like image 262
hyperrjas Avatar asked Apr 17 '26 21:04

hyperrjas


1 Answers

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/

like image 83
kupriyanenko Avatar answered Apr 20 '26 11:04

kupriyanenko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!