Skip to content

Commit d459e76

Browse files
add article: js_bind-call-apply
1 parent c9799dd commit d459e76

File tree

2 files changed

+89
-1
lines changed

2 files changed

+89
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@
8888
8989
> [进程和线程](https://github.com/programmer-zhang/front-end/tree/master/profiles/js_进程和线程.md)
9090
91-
> [手写 bind、call、apply 函数]
91+
> [手写 bind、call、apply 函数](https://github.com/programmer-zhang/front-end/tree/master/profiles/js_bind-call-apply.md)
9292
9393
> [解决 JS 绑定事件多次执行](https://github.com/programmer-zhang/front-end/tree/master/profiles/js_more_times_bind_events.md)
9494

profiles/js_bind-call-apply.md

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# 手写 bind、call、apply 函数
2+
3+
> 在 JavaScript 中,`bind`, `call`, 和 `apply` 是三种用于控制函数上下文(`this` 关键字)和参数传递的重要方法。这些方法允许你在调用函数时明确指定函数运行时的上下文以及传递参数。在本文中,我们将学习如何手写这三个函数的实现。
4+
5+
> 本篇文章使用 ChatGPT 辅助编写
6+
7+
## 阅读本文您将收获
8+
* `bind`, `call`, 和 `apply` 三种函数的作用
9+
* `bind`, `call`, 和 `apply` 三种函数的手撸方式
10+
11+
## 1. `bind` 函数
12+
13+
* `bind` 方法用于创建一个新的函数,该函数在调用时具有指定的上下文,同时也可以传递参数。以下是一个手写 `bind` 函数的示例:
14+
15+
```javascript
16+
Function.prototype.myBind = function (context, ...args) {
17+
const originalFunction = this;
18+
return function (...innerArgs) {
19+
return originalFunction.apply(context, args.concat(innerArgs));
20+
};
21+
};
22+
23+
const person = {
24+
name: "John"
25+
};
26+
27+
function greet(greeting) {
28+
console.log(`${greeting}, ${this.name}`);
29+
}
30+
31+
const greetPerson = greet.myBind(person, "Hello");
32+
greetPerson();
33+
// 输出:Hello, John
34+
```
35+
36+
* 在上述示例中,`myBind` 方法接受一个上下文 `context` 和任意数量的参数 `args`,然后返回一个新的函数,该函数将指定的上下文和参数传递给原始函数。
37+
38+
## 2. `call` 函数
39+
40+
* `call` 方法用于立即调用一个函数,并指定函数的上下文以及参数。以下是一个手写 `call` 函数的示例:
41+
42+
```javascript
43+
Function.prototype.myCall = function (context, ...args) {
44+
return this.apply(context, args);
45+
};
46+
47+
const person = {
48+
name: "Alice"
49+
};
50+
51+
function introduce(age) {
52+
console.log(`My name is ${this.name} and I am ${age} years old.`);
53+
}
54+
55+
introduce.myCall(person, 30);
56+
// 输出:My name is Alice and I am 30 years old.
57+
```
58+
59+
* 在上述示例中,`myCall` 方法接受一个上下文 `context` 和任意数量的参数 `args`,然后使用 `apply` 方法来调用原始函数,并传递上下文和参数。
60+
61+
## 3. `apply` 函数
62+
63+
* `apply` 方法用于立即调用一个函数,但与 `call` 不同,它接受参数的形式为数组。以下是一个手写 `apply` 函数的示例:
64+
65+
```javascript
66+
Function.prototype.myApply = function (context, args) {
67+
return this.call(context, ...args);
68+
};
69+
70+
const person = {
71+
name: "Bob"
72+
};
73+
74+
function saySomething(words) {
75+
console.log(`${this.name} says: ${words}`);
76+
}
77+
78+
saySomething.myApply(person, ["Hello, World!"]);
79+
// 输出:Bob says: Hello, World!
80+
```
81+
82+
* 在上述示例中,`myApply` 方法接受一个上下文 `context` 和一个参数数组 `args`,然后使用 `call` 方法来调用原始函数,并展开参数数组以传递给原始函数。
83+
84+
## 结论
85+
86+
手写 `bind`, `call`, 和 `apply` 函数可以帮助你更好地理解这些方法的工作原理。这些方法是 `JavaScript` 中非常有用的工具,用于控制函数的执行上下文和参数传递。通过了解它们的内部实现,你可以更好地掌握它们的用法和灵活性。
87+
88+
希望本文中的示例有助于你深入了解这些方法,并在日常编程中更好地运用它们。通过手写这些函数,你可以更好地理解 JavaScript 函数调用的背后原理,以及如何更好地控制函数的行为。

0 commit comments

Comments
 (0)