清淨
  • Java

    • Java 基础
  • 框架

    • Flyway
    • MapStruct
    • Spring Cloud
  • 中间件

    • Elasticsearch
    • Redis
    • RabbitMQ
    • Kafka
  • 三剑客

    • HTML
    • CSS
    • JavaScript
  • 进阶

    • jQuery
    • ECMAScript6
    • TypeScript
  • Vue

    • Vue2
    • Vue3
    • VuePress
  • Linux
  • Jenkins
  • Maven
  • MySQL
  • 插件工具
  • 代码集成
  • 其它

    • 数据结构与算法
    • 计算机网络
    • 操作系统
    • 设计模式
迁移日志
订阅号
微信订阅号
微信订阅号
GitHub
  • Java

    • Java 基础
  • 框架

    • Flyway
    • MapStruct
    • Spring Cloud
  • 中间件

    • Elasticsearch
    • Redis
    • RabbitMQ
    • Kafka
  • 三剑客

    • HTML
    • CSS
    • JavaScript
  • 进阶

    • jQuery
    • ECMAScript6
    • TypeScript
  • Vue

    • Vue2
    • Vue3
    • VuePress
  • Linux
  • Jenkins
  • Maven
  • MySQL
  • 插件工具
  • 代码集成
  • 其它

    • 数据结构与算法
    • 计算机网络
    • 操作系统
    • 设计模式
迁移日志
订阅号
微信订阅号
微信订阅号
GitHub
  • ECMAScript6

    • 简介
    • let
    • const
    • 解构赋值
    • 对象简写
    • 箭头函数
    • 参数默认值
    • 剩余参数
    • 可选链操作符
    • 空值合并运算符
    • 逻辑空赋值
    • 模板字符串
    • 扩展运算符 (展开语法)
    • Array 扩展
    • String 扩展
    • Number 扩展
    • Object 扩展
    • 迭代器
    • 生成器
    • BigInt
    • Set
    • Map
    • class
    • 模块化
    • globalThis
    • Promise
    • async-await

箭头函数

ES6 中可以使用箭头函数来简化函数的定义。需要注意的是,箭头函数中没有 this。

let foo = () => {
  console.log('foo')
}
foo()

箭头函数中的 this 是函数在定义时,向外查找到的最近的 this。

let a = {name: 'lo'}
function foo() {
  console.log(this.name)
}
foo.call(a) // lo
/* ------------------------------- */
let a = {name: 'lo', age: 1}
let bar = () => {
  console.log(this.age)
}
bar.call(a) // undefined
/* ------------------------------- */
let age = 1
let obj = {
  age: 100,
  say: () => {
    console.log(this.age)
  }
}
obj.say() // 1
setTimeout(function () {
  console.log(this) // Window
}, 1000)
setTimeout(() => {
  console.log(this) // Window
}, 1000)

const obj = {
  data() {
    setTimeout(function () {
      console.log(this) // window
    }, 1000)
    setTimeout(() => {
      console.log(this) // data,即 obj 这个对象
    }, 1000)
  }
}
obj.data()

// 结论如下
// 箭头函数中的 this 会往外查找,直到找到最近的有 hits 的定义为止,这时箭头函数的 this 就指向这个最近的 this。

const obj = {
  data() {
    setTimeout(function () {
      setTimeout(function() {
        console.log(this) // Window
      }, 1000)
      setTimeout(() => {
        console.log(this) // Window
      }, 1000)
    },1000)
    setTimeout(() => {
      setTimeout(function() {
        console.log(this) // Window
      }, 1000)
      setTimeout(() => {
        console.log(this) // obj
      }, 1000)
    },1000)
  }
}
obj.data()
// 定义函数
const fun = (参数) => { }

// 只有一个参数时,小括号可省
const fun = x => { }

// 只有一条语句参数时,大括号可省,它会自动将语句执行并返回结果
const fun = x => console.log(x) // 返回值是 undefined
const fun = (x, y) => x + y // 返回 x + y

// 在将函数作为参数时,会大量使用箭头函数来进行简化
const app = new Vue({
  el: '#app',
  methods: {
    // 以下几种函数的定义是等价的
    add() {
        
    },
    reduce: function() {
        
    },
    query: () => {}
  }
})

注意:

  • 箭头函数中没有 arguments
  • 箭头函数不能作为构造函数使用
想要编辑此页?
上次更新: 2025/6/5 23:39
贡献者: Junfeng Dai
←对象简写参数默认值→
[ 纸上得来终觉浅绝知此事要躬行 ]
Copyright © 2021-present Junfeng Dai
蜀ICP备2021009537号