Skip to content

ES6 之箭头函数 #56

Open
Open
@myLightLin

Description

@myLightLin

箭头函数的基本语法:

const foo = (name) => name

箭头函数与普通函数的对比:

  1. 箭头函数没有自己的 this,它的 this 是通过词法作用域来确定的。既然没有 this,那 call,apply,bind 这些自然就无效了
// 平时我们经常会使用 self 来保存嵌套函数的 this ,使用箭头函数就不需要这样了
function A() {
  const el = document.getElementById('el')
  el.addEventListener('click',  () => {
    
  })
}
  1. 箭头函数没有 arguments ,如果想访问参数,可以通过命名参数或 rest 参数来访问:
const foo = (...params) => {
  console.log('params', params)
}
  1. 箭头函数不能被 new 关键字调用
  2. 既然不能被 new 调用,箭头函数也就没有 prototype 这个原型属性,同样 super 也没有

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions