Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can CoffeeScript Be Translated into This Piece of JavaScript?

function abc() {
    var a = 1;
    var func = function() {
        var a = 2;
    }
    func();
    alert(a);
}

Pay attention to the var, in the piece of code, the result of a will be 1, but if the var is omitted, the result will be 2, but I found Coffee not able to translate to this.

For example the following:

abc = ->
    a = 1
    func = ->
        a = 2
        return
    func()
    alert(a)
    return
like image 282
tangrui Avatar asked Dec 17 '22 02:12

tangrui


1 Answers

From the CoffeeScript docs (emphasis added):

Because you don't have direct access to the var keyword, it's impossible to shadow an outer variable on purpose, you may only refer to it.

Is there a reason you need to shadow a and can't just use a different identifier?

like image 123
James Allardice Avatar answered Feb 15 '23 11:02

James Allardice