美文网首页
箭头函数的特性

箭头函数的特性

作者: 拾光逐梦 | 来源:发表于2019-02-18 16:03 被阅读0次

1、 函数体内的this值,绑定的定义时所在的作用域的this

<script>
  document.onclick = function () {
    setTimeout(function(){
      var that = this
      console.log(this)      // this => window
      console.log(that)     // that => 被点击的元素
    })
   // 箭头函数中this绑定的是所在定义的作用域中的this
   setTimeout (() => {
     console.log(this);      // this => 被点击的元素
   },1000)
  }


  document.onclick = () => {
    setTimeout(() => {
        console.log(this)      // this => window
    })
  }
</script>

2、不可以当作构造函数
3、不可以使用arguments对象

function fn(a, ...arr){    //rest参数,把实参放在数组中
  console.log(a)      // 1
  console.log(arr)    // [1, 2, 3]
}
fn(1,2,3)

相关文章

网友评论

      本文标题:箭头函数的特性

      本文链接:https://www.haomeiwen.com/subject/wqnaeqtx.html