zoukankan      html  css  js  c++  java
  • JS 箭头函数与普通函数的区别

    区别

    1. 箭头函数体内的this对象,就是定义时所在的对象,而不是使用时所在的对象。
    2. 箭头函数没有arguments,如果要用,可以用 rest 参数代替 (注意在node环境下是有arguments的)
    3. 箭头函数不能作为构造函数,不能使用new
    4. 箭头函数没有原型,不能继承
    5. 箭头函数不能当做Generator函数,不能使用yield关键字

    验证示例

    • 浏览器侧验证
      <script>
          const test1=(...numbers)=>{
              console.log(this)
              console.log(numbers)      
              console.log(arguments)
          };
          const test2=function(){
              console.log(this)
              console.log(arguments)
          }
    
          test1(123);//分别输出 window [123] 报错
          test2(123);//分别输出 window Arguments 
      </script>
    
    • node侧验证
    const test1=(...numbers)=>{
      console.log(this)
      console.log(numbers)      
      console.log(arguments)
    };
    const test2=function(){
      console.log(this)
      console.log(arguments)
    }
    
    test1(123);//分别输出 global [123] Arguments
    test2(123);//分别输出 global Arguments 
    

    参考

  • 相关阅读:
    异常
    抽象类
    java基础—接口3
    java基础—接口2
    java基础—接口1
    Android拍照适配方案
    继承—Car
    继承—矩形
    使用CSS和jQuery实现tab页
    纯CSS实现圆形进度条
  • 原文地址:https://www.cnblogs.com/xingguozhiming/p/13407417.html
Copyright © 2011-2022 走看看