Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
vegetalbirddd committed Nov 13, 2023
1 parent c771d13 commit fef7bff
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion docs/面试/NONI/JS/JS基础/手写call.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
### **call()**

调用call的函数挂载到传入的第一个参数上,再通过该参数对象来调用‘调用call的函数’,并且能够得到传入其他参数后执行得到的结果

例如:Fn.call(context, a,b),函数Fn挂载到context对象上,通过context对象调用函数Fn,并且能够得到传入参数a,b后执行得到的结果

### **手写**
```js
Function.prototype.myCall = function (context) {
if (context === null || context === undefined) {
Expand Down Expand Up @@ -53,4 +60,15 @@ fn1.myCall(1, 1, 2);
fn1.myCall("str", 1, 2);
fn1.myCall(true, 1, 2);

```
```

### **简单版本**

```js
Function.prototype.myCall = function(context){
context.fn = this
context.fn();
delete context.fn
}

```

0 comments on commit fef7bff

Please sign in to comment.