简介
- let和var的区别
let声明的变量是block-scoped(块级作用域)
- 箭头函数
- Const常量
- 箭头函数
- Class类默认参数
- 模板文字
- 结构语法
- Import和Export
- Promise
- Rest参数
箭头函数
```c let x = (参数1,参数2,…参数N)=>{ //函数体 } let x = function(x,y){ return xy; } //等效于 let x = (x,y)=>xy //等效于 let x = (x,y)=>{ return x*y; }
//箭头函数作为表达式 let age = 5; let welcome = (age<18)?()=>console.log(“小于”):()=>console.log(“大于”)
<a name="bpNiV"></a>#### 实例```cconsole.log("普通函数指向")function persian(){this.age = 25,this.func = function(){console.log(this.age);function test(){console.log(this.age);}test();}}let x = new persian();x.func();//25,undefinedconsole.log("箭头函数指向");function persian2(){this.age = 30,this.func = function(){console.log(this.age);let test= ()=>{console.log(this.age);}test();}}let y = new persian2();y.func();//30,30
Promise箭头函数
asyncFunction().then(function(){return asyncFunction();}).then(function(){return asyncFunction2();}).then(function(){finish})asyncFunction().then(()=>{asyncFunction1()}).then(()=>{asyncFunction2();}).then(()=>finish;)
普通和箭头函数的差别
普通函数有arguments参数(有参数绑定),箭头函数没有arguments(无参数绑定)
let argfun = function(){console.log(arguments);}argfun(1,2,3); //Arguments(3)数组let argfunj = (...n){//不使用spread语法就会报错,因为箭头函数没有arguments变量console.log(n);}argfunj(1,2,3);// [1,2,3]
注意事项
不应使用箭头函数在对象内部创建方法
let person = {age:25,sayName:()=>{console.log(this.age)}}person.sayName();//undefined
不能将箭头函数用作构造函数
let Foo = ()=>{};let foo = new Foo();//typeError: Foo is not a costructor
模板文字
表达式插值 ==>${表达式}
//不使用插值语法const name = "Jack";console.log("Hello"+ name);//Hello Jack//使用插值语法const name = "jack";console.log(`Hello ${name}`);
标记模板
//原生写法function tagExample(strings){}
