简介

  1. let和var的区别

let声明的变量是block-scoped(块级作用域)

  1. 箭头函数
  2. Const常量
  3. 箭头函数
  4. Class类默认参数
  5. 模板文字
  6. 结构语法
  7. Import和Export
  8. Promise
  9. 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(“大于”)

  1. <a name="bpNiV"></a>
  2. #### 实例
  3. ```c
  4. console.log("普通函数指向")
  5. function persian(){
  6. this.age = 25,
  7. this.func = function(){
  8. console.log(this.age);
  9. function test(){
  10. console.log(this.age);
  11. }
  12. test();
  13. }
  14. }
  15. let x = new persian();
  16. x.func();
  17. //25,undefined
  18. console.log("箭头函数指向");
  19. function persian2(){
  20. this.age = 30,
  21. this.func = function(){
  22. console.log(this.age);
  23. let test= ()=>{
  24. console.log(this.age);
  25. }
  26. test();
  27. }
  28. }
  29. let y = new persian2();
  30. y.func();
  31. //30,30

Promise箭头函数

  1. asyncFunction().then(function(){
  2. return asyncFunction();
  3. }).then(function(){
  4. return asyncFunction2();
  5. }).then(function(){
  6. finish
  7. })
  8. asyncFunction().then(()=>{
  9. asyncFunction1()
  10. }).then(()=>{
  11. asyncFunction2();
  12. }).then(()=>finish;)

普通和箭头函数的差别

  1. 普通函数有arguments参数(有参数绑定),箭头函数没有arguments(无参数绑定)

    1. let argfun = function(){
    2. console.log(arguments);
    3. }
    4. argfun(1,2,3); //Arguments(3)数组
    5. let argfunj = (...n){
    6. //不使用spread语法就会报错,因为箭头函数没有arguments变量
    7. console.log(n);
    8. }
    9. argfunj(1,2,3);// [1,2,3]

    注意事项

  2. 不应使用箭头函数在对象内部创建方法

    1. let person = {
    2. age:25,
    3. sayName:()=>{
    4. console.log(this.age)
    5. }
    6. }
    7. person.sayName();//undefined
  3. 不能将箭头函数用作构造函数

    1. let Foo = ()=>{};
    2. let foo = new Foo();//typeError: Foo is not a costructor

    模板文字

    表达式插值 ==>${表达式}

    1. //不使用插值语法
    2. const name = "Jack";
    3. console.log("Hello"+ name);//Hello Jack
    4. //使用插值语法
    5. const name = "jack";
    6. console.log(`Hello ${name}`);

    标记模板

    1. //原生写法
    2. function tagExample(strings){}