let 和const的变量作用域

2019-07-30  本文已影响0人  oneways

下面这个例子(http://sina.lt/fQNW)来理解let或const关键字声明的变量如何工作。

let movie ="Lord of the Rings";
//var movie = "Batman v Superman";

function starWarsFan(){
    const movie = 'Star Wars';
    return movie;
}

function marvelFan(){
    movie = "The Avengers";
    return movie;
}
function blizzardFan(){
    const isFan = true ;
    let phrase = 'Warcraft';
    console.log('Before if:'+ phrase);
    if(isFan){
        let phrase = 'initial text';
        phrase = 'For the Horde!';
        console.log('After if:'+ phrase);
    }
    phrase = 'For the Alliance!';
    console.log('After if:' + phrase);
}
console.log(movie);//Lord of Rings
console.log(starWarsFan());//Star Wars
console.log(marvelFan());//The Avengers
console.log(movie);//The Avengers
blizzardFan();//Before if: Warcraft Inside if : For the Horde! After if : For the Alliance!
上一篇 下一篇

猜你喜欢

热点阅读