码迷,mamicode.com
首页 > 其他好文 > 详细

[ES6] 22. Const

时间:2015-04-29 07:10:13      阅读:125      评论:0      收藏:0      [点我收藏+]

标签:

‘const‘ keyword is for creating a read only variable, something you can never change once created.

‘const‘ likes ‘let‘ keyword alos has block scope.

describe("using const", function(){

    it("will make a variable read-only", function(){
    
        const MAX_SIZE = 10;
        
        //MAX_SIZE = 12; //SyntaxError
        
        expect(MAX_SIZE).toBe(10);  //true
    });
    
    it("can shadow outer declaration", function(){
        
        const x = 12;
        var doWork = function(){
            
            let x = 10;
            return x;
        };
        
        var result = doWork();
        expect(result).toBe(10);  //true
        expect(x).toBe(12); //true
    });
    
    it("const also has block scope", function(){
        
        if(true){
            const x = 12;  //SyntaxError, x is not defined
        }
        
        var doWork = function(){
            
            let x = 10;
            return x;
        };
        
        var result = doWork();
        expect(result).toBe(10);  //true
        expect(x).toBe(12); //true
    });    
});

 

[ES6] 22. Const

标签:

原文地址:http://www.cnblogs.com/Answer1215/p/4464559.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!