let 和const的变量作用域

下面这个例子(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!
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容