1.let 和const
let块级作用域 相当于var
{
let a = 10;
var b = 1;
}
a //not defined
b //1
a在外部就为未定义;
for循环中就很适合用let
for(let i = 1;i<10;i++){
//do something..
}
console.log(i);//i is not defined;
const声明一个只读的常亮。声明后就不可改变,防止无意间修改变量所导致错误
2.模板对象
1 2 | var name = 'Your name is ' + first + ' ' + last + '.'; var url = 'http://localhost:3000/api/messages/' + id; |
幸运的是,在ES6中,我们可以使用新的语法$ {NAME},并把它放在反引号里
:1 2 | var name = `Your name is ${first} ${last}. `; var url = `http://localhost:3000/api/messages/${id}`; |
3.多行字符串
var roadPoem = 'Then took the other, as just as fair,nt' + 'And having perhaps the better claimnt' + 'Because it was grassy and wanted wear,nt' + 'Though as for that the passing therent' + 'Had worn them really about the same,nt'; var fourAgreements = 'You have the right to be you.n You can only be you when you do your best.'; |
然而在ES6中,仅仅用反引号就可以解决了:
1 2 3 4 5 6 7 | var roadPoem = `Then took the other, as just as fair, And having perhaps the better claim Because it was grassy and wanted wear, Though as for that the passing there Had worn them really about the same,`; var fourAgreements = `You have the right to be you. You can only be you when you do your best.`; |