九个ES6新增语法的使用和演示
为了测试使用class定义类来便于调用测试,代码可以之间粘贴复制使用
Let声明关键字
Const声明关键字
箭头函数
默认参数
For…of…循环
展开属性
Map集合
静态方法
Getter和Setter方法
class Method {
//构造方法
constructor(){
Method.a = 1;
}
//测试方法
static test(){
return this.a;
}
//let类似于var但是let具有作用域
static let(){
{
var a = 1;
let b = 2;
console.log(a);//1
console.log(b);//2
}
//使用try{}catch(){}为了测试结果能顺利演示
try{
console.log(a);//1
console.log(b);//undefined
}catch (e) {
// throw new Error("作用域有问题");
}
}
/**
* const定义的变量无法更改这个值
* 但是定义的数组、者对象、
* 集合内部的值是可以更新的
*/
static Const(){
//初始化参数
const a = 1;
//数组
const arr = ['Larry' , 'Marry'];
//集合
const map = new Map();
//对象
const obj = {
name : 'Larry'};
return{
Let : function () {
try{
console.log(a);
a = 2;//异常Assignment to constant variable.
console.log(a);//undefined
}catch (e) {
//console.log(e);//打印异常
}
},
Arr : function () {
//数组
console.log