JavaScript中apply, bind, call 的区别

apply、bind 和 call 是 JavaScript 中的三个方法,都是用来控制函数的 this 指向的。它们有相似之处,但在传递参数和调用方式上有一些区别。我们可以逐一看看它们的定义和使用场景。 1. call 方法 语法:func.call(thisArg, arg1, arg2, …) 示例 在这里,call 方法将 this 设置为 person 对象,并将 “Hello” 和 “!” 作为参数传递给 greet 函数。 2. apply 方法 语法:func.apply(thisArg, [argsArray]) 示例 在这里,apply 的作用与 call 类似,但传递参数的方式不同。apply 使用一个数组来传递参数,而 call 使用的是一组独立的参数。 3. bind 方法 语法:func.bind(thisArg, arg1, arg2, …) 示例 在这里,bind 创建了一个新函数 boundGreet,这个函数的 this 永远绑定为 person,并预设了第一个参数 […]

Read More

The Best Ways to Do Market Research For Your Business Plan.

There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don’t look even slightly believable. If you are going to use a passage of Lorem Ipsum. You need to be sure there isn’t anything embarrassing hidden in the middle […]

Read More

Why We Don’t Have Technical Interviews for Technical Roles at Buffer.

There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don’t look even slightly believable. If you are going to use a passage of Lorem Ipsum. You need to be sure there isn’t anything embarrassing hidden in the middle […]

Read More