Created
August 12, 2016 18:00
-
-
Save vinacms/a31b54cbf29c02db7e7b142b293f983d to your computer and use it in GitHub Desktop.
#ES6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const dùng để khai báo một hằng số - là một giá trị không thay đổi được trong suốt quá trình chạy. | |
const A = 5; | |
A = 10; // Lỗi Uncaught TypeError: Assignment to constant variable | |
let tạo ra một biến chỉ có thể truy cập được trong block bao quanh nó, khác với var - tạo ra một biến có phạm vi truy cập xuyên suốt function chứa nó. | |
function foo() { | |
let x = 10; | |
if (true) { | |
let x = 20; // x này là x khác rồi đấy | |
console.log(x); // in ra 20 | |
} | |
console.log(x); // in ra 10 | |
} | |
khi ở global scope (tức là không nằm trong một function nào cả), từ khóa var tạo ra thuộc tính mới cho global object (this), còn let thì không: | |
var x = 'global'; | |
let y = 'global'; | |
console.log(this.x); // "global" | |
console.log(this.y); // undefined | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment