1.函数作为参数传值,后面加个括号则为立即执行该函数后,返回结果作为参数传值
例如:
function func1(cb) {
console.log('func1')
}
function func2(arg1, arg2) {
console.log('func2')
return function() {
console.log('func3')
}
}
func1(func2('arg1', 'arg2'))
//打印结果:func2 func1
2.箭头函数没有自己的执行上下文,也没有arguments,做参数传递也不能通过call绑定this,细品
let that = this
function a(func) {
let aa = 2
console.log(that === this)
func.call(this, aa, this)
}
a(function(aa, that) {
console.log(aa, that === this)
})
// 打印结果:
// false
// 2 true
let that = this
function a(func) {
let aa = 2
console.log(that === this)
func.call(this, aa, this)
}
a((aa, that) => {
console.log(aa, that === this)
})
// 打印结果:
// false
// 2 false
let that = this
function a(func) {
let aa = 2
console.log(that === this)
func.call(this, aa, this)
}
a((aa, _that) => {
console.log(aa, that === this)
})
// 打印结果:
// false
// 2 true
网友评论