apply、call、bind的区别,这可能是全网最简单的区分方法

啥都不说,先上代码,看了秒懂...

let arr = [1,2,3,4,5,6,7,8,9]

console.log(Math.max.apply(this,arr)) // 9
console.log(Math.max.call(this,...arr)) // 9
console.log(Math.max.bind(this,...arr)) // ƒ max() { [native code] }

let max = Math.max.bind(this,...arr)
console.log(max()) // 9

 

语法分析:

fn.apply(this,arr)

fn.call(this,arr[0],arr[1]...)

fn.bind(this,arr[0],arr[1]...)

总结 apply、call、bind

1. 第一个参数都是 this

2. apply 第二个参数是数组,call、bind是队列

3. call直接调用,bind返回的是一个函数,需要调用一下

posted @ 2022-11-30 00:30  cros  阅读(492)  评论(0)    收藏  举报