1. 程式人生 > >es6 let和const

es6 let和const

ons defined ria ole tar iss 聲明 htm love

一、let

1、let塊作用域

if(true){
            var a=1;
            let b=2;
        }
        console.log("a:"+a);//a:1
        console.log("b:"+b);//Uncaught ReferenceError: b is not defined

let常用於for循環

二、const

1、const聲明的變量也是塊作用域,但是不能被多次修改

let a=10;
        const b=20;
        console.log(a);
        console.log(b);
        a
=100; b=200;//Uncaught TypeError: Assignment to constant variable.

2、const 聲明變量時必須賦值

const a;//Uncaught SyntaxError: Missing initializer in const declaration

3、const可以指定為對象

const常亮指向對象的指針不能變,對象本身是可以改變的

let user={name:"starof",age:25};
        const LOVE_YOU=user;
        console.log(user);
        
/*LOVE_YOU=1;//報錯*/ user.age=18; console.log(user);

技術分享

本文作者starof,因知識本身在變化,作者也在不斷學習成長,文章內容也不定時更新,為避免誤導讀者,方便追根溯源,請諸位轉載註明出處:http://www.cnblogs.com/starof/p/6919624.html有問題歡迎與我討論,共同進步。

es6 let和const