From 919c9d6d8387a88258bb947579e805b5581b211a Mon Sep 17 00:00:00 2001 From: ellis Date: Mon, 22 May 2023 19:44:53 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=89=8B=E5=86=99=20bind?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...72\347\241\200\347\237\245\350\257\206.md" | 3 + ...346\211\213\345\206\231call&apply&bind.md" | 68 +++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 "\345\211\215\347\253\257\351\235\242\350\257\225\347\254\224\350\256\260/\346\211\213\345\206\231call&apply&bind.md" diff --git "a/\345\211\215\347\253\257\351\235\242\350\257\225\347\254\224\350\256\260/React \345\237\272\347\241\200\347\237\245\350\257\206.md" "b/\345\211\215\347\253\257\351\235\242\350\257\225\347\254\224\350\256\260/React \345\237\272\347\241\200\347\237\245\350\257\206.md" index 016fda5..d6f3ef1 100644 --- "a/\345\211\215\347\253\257\351\235\242\350\257\225\347\254\224\350\256\260/React \345\237\272\347\241\200\347\237\245\350\257\206.md" +++ "b/\345\211\215\347\253\257\351\235\242\350\257\225\347\254\224\350\256\260/React \345\237\272\347\241\200\347\237\245\350\257\206.md" @@ -135,3 +135,6 @@ constructor => getDerivedStateFromProps => render => 【更新DOM和refs】 => ### 卸载阶段: componentWillUnmount + + + diff --git "a/\345\211\215\347\253\257\351\235\242\350\257\225\347\254\224\350\256\260/\346\211\213\345\206\231call&apply&bind.md" "b/\345\211\215\347\253\257\351\235\242\350\257\225\347\254\224\350\256\260/\346\211\213\345\206\231call&apply&bind.md" new file mode 100644 index 0000000..23570ea --- /dev/null +++ "b/\345\211\215\347\253\257\351\235\242\350\257\225\347\254\224\350\256\260/\346\211\213\345\206\231call&apply&bind.md" @@ -0,0 +1,68 @@ +# 手写call、apply、bind + +> 引用:https://juejin.cn/post/6962037968013885448 + +实现思想: + +call/apply/bind 都是用来改变函数体内的 this 指向的。我们都知道`谁调用函数,this 就指向谁`。 + +那大体思路就是,把函数`fn`设置成某一个对象`obj`的属性,然后执行 `obj.fn()` 不就把 `this` 指向到 `obj` 了么。 + + + +## call + +call 的语法:`function.call(thisArg, arg1, arg2, ...)` + +```js +Function.prototype.myCall = function(thisArg, ...rest) { + const context = thisArg ?? window; + context.fn = this; + const result = context.fn(...rest); + delete context.fn; + return result; +} +``` + + + +## apply + +apply 语法: `function.apply(thisArg)` `function.apply(thisArg, argsArray)` + +```js +Function.prototype.myApply = function(thisArg, argsArray = []) { + const context = thisArg ?? window; + context.fn = this; + if (!Array.isArray(argsArray)) { + throw new Error('第二个参数请传入一个数组类型') + } + const result = context.fn(...argsArray) + delete context.fn; + return result; +} +``` + + + +## bind + +**`bind()`** 方法创建一个新的函数,在 `bind()` 被调用时,这个新函数的 `this` 被指定为 `bind()` 的第一个参数,而其余参数将作为新函数的参数,供调用时使用。 + +bind 语法: `function.bind(thisArg[, arg1[, arg2[, ...]]])` + +```js +Function.prototype.myBind = function(thisArg, ...args) { + const context = thisArg ?? window; + context.fn = this; + + return function(...args2) { + const args = [...args, ...args2]; + const result = context.fn(...args); + delete context.fn; + return result; + } +} + +``` + -- Gitee