본문 바로가기

개발/Javascript

Understanding ECMAScript 6

https://leanpub.com/understandinges6/read


쭉쭉 코드만 살펴보고 처음 보거나 이해 안 가는 것은 내용도 읽어봄


Let Declarations

let문법은 var와 같음.


let 다른점

호이스팅 안 됨.

현재 code block({})에서만 유효함


같은 스콥에서 var, let으로 동일한 변수명 사용할 수 없음

아래는 가능

var count = 30;

// Does not throw an error
if (condition) {

    let count = 40;

    // more code
}


Constant Declarations

변하지 않을 값을 정의할 때 사용. 그래서 무조건 초기화 필요

let과 마찬가지로 현재 block에서만 유효함. 호이스팅 안 됨.

같은 스콥에서 var, let으로 선언한 같은 변수명 쓰면 안 됨.

const maxItems = 30;


Bock Binding in Loops

i는 loop안에서만 존재한다.

for (let i=0; i < 10; i++) {
    process(items[i]);
}

// i is not accessible here - throws an error
console.log(i);


Let Declarations is Loops

var은 다 10나옴

각 순회할 때마다, let은 새로운 변수 i를 선언하고, 초기화 한다.

var funcs = [];

for (let i=0; i < 10; i++) {
    funcs.push(function() {
        console.log(i);
    });
}

funcs.forEach(function(func) {
    func();     // outputs 0, then 1, then 2, up to 9
})

global block bindings 할 차례





'개발 > Javascript' 카테고리의 다른 글

[webpack] 읽을만한 자료들  (0) 2016.07.11
자바스크립트 모듈(module) 이해하기  (0) 2016.07.06
웹 성능 최적화  (0) 2015.08.11
ecmascript6 추가 사항  (0) 2015.06.10
airbnb javascipt guide  (0) 2015.06.10