首页 > 技术文章 > apply() && call()

lankongclub 2019-09-12 17:25 原文

每个函数都有 apply() 和 call() 方法
apply():调用函数,参数 给定 this + 数组
call():和 apply() 方法类似,参数 给定this + 参数列表

const arr = [2, 3, 4]
const obj = {
  lalala: 234
}
function func(a, b, c) {
  console.log(a, b, c)
  console.log(this.lalala)
}
func.apply(obj, arr) // 2 3 4 , 234
func.call(obj, ...arr) // 2 3 4, 234
func(...arr) // 2 3 4, undefined

 

推荐阅读