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 

参考

posted @ 2020-07-31 00:15  mingL  阅读(2344)  评论(0编辑  收藏  举报