首页 > 其他 > 详细

[ES6] 22. Const

时间:2015-04-29 07:09:00      阅读:266      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!