Javascript变量提升(“已定义”jshint错误)

时间:2016-02-26 15:14:58

标签: javascript jshint

由于javascript变量提升,jshint抱怨:

function test() {
    ...
    if( cond1 ) {
        var a = ...
        //do something with a
    }
    ...
    if( cond2 ) {
        var a = ...
        //do something with a
    }
    ...
}

因为它相当于:

function test() {
    var a;
    var a;
    ...
    if( cond1 ) {
        a = ...
        //do something with a
    }
    ...
    if( cond2 ) {
        a = ...
        //do something with a
    }
    ...
}

第一种方法是否会导致某些代码出现错误?

0 个答案:

没有答案