05 # 手写 bind

简介: 05 # 手写 bind

ac41ca606dfc478083af2533230e71a3.pngbind 干了什么?

  1. 改变 this 指向
  2. 没有让函数执行,返回一个改变 this 指向后的函数

bind 难点在于参数的收集

手写 bind

简单实现如下:

<script>
    Function.prototype.kaimoBind = function (content) {
        // 获取到 bind 里的剩余参数
        let bindArgs = Array.prototype.slice.call(arguments, 1);
        console.log("bindArgs----->", bindArgs);
        // 这里的 this 指向调用 kaimoBind 的 fn
        let that = this;
        function gn() {
            // 拿到 bind 返回函数的参数
            let gnArgs = Array.prototype.slice.call(arguments);
            console.log("gnArgs----->", gnArgs);
            // 改变 fn 的 this 指向
            return that.apply(content, bindArgs.concat(gnArgs));
        }
        return gn;
    };
    function fn(...args) {
        console.log("fn----this----->", this);
        console.log("fn----args----->", args);
        return args.join("");
    }
    let obj = {
        name: "kaimo313"
    };
    let res = fn.bind(obj, "k", "a", "i", "m", "o");
    console.log("res---bind-->", res(3, 1, 3));
    let res2 = fn.kaimoBind(obj, "k", "a", "i", "m", "o");
    console.log("res2---kaimoBind-->", res2(3, 1, 3));
</script>


8c2eafaaa25a4d4c91f3eeac4f981e58.png

复杂一点实现如下:

<script>
    Function.prototype.kaimoBind = function (content) {
        // 获取到 bind 里的剩余参数
        let bindArgs = Array.prototype.slice.call(arguments, 1);
        console.log("bindArgs----->", bindArgs);
        // 这里的 this 指向调用 kaimoBind 的 fn
        let that = this;
        function gn() {
            // 拿到 bind 返回函数的参数
            let gnArgs = Array.prototype.slice.call(arguments);
            console.log("gnArgs----->", gnArgs);
            // 改变 fn 的 this 指向
            return that.apply(content, bindArgs.concat(gnArgs));
        }
        function Mn() {}
        Mn.prototype = this.prototype;
        // gn 继承 Mn
        gn.prototype = new Mn();
        return gn;
    };
    function fn(...args) {
        console.log("fn----this----->", this);
        console.log("fn----args----->", args);
        return args.join("");
    }
    fn.prototype.kaimo = 313;
    let obj = {
        name: "kaimo313"
    };
    let res = fn.bind(obj, "k", "a", "i", "m", "o");
    console.log("res---bind-->", res(3, 1, 3));
    let res2 = fn.kaimoBind(obj, "k", "a", "i", "m", "o");
    console.log("res2---kaimoBind-->", res2(3, 1, 3));
    let res3 = new res2();
    console.log("res3----->", res3.kaimo);
</script>


ac41ca606dfc478083af2533230e71a3.png



目录
相关文章
|
22天前
|
存储 算法 对象存储
【C++入门到精通】function包装器 | bind() 函数 C++11 [ C++入门 ]
【C++入门到精通】function包装器 | bind() 函数 C++11 [ C++入门 ]
19 1
|
10月前
|
前端开发 Shell
前端笔记之手写bind
前端笔记之手写bind
36 0
|
22天前
|
资源调度 JavaScript 前端开发
js高级进价 - 手写bind
js高级进价 - 手写bind
|
22天前
|
索引
08 # 手写 filter 方法
08 # 手写 filter 方法
24 0
|
8月前
|
SQL Java 关系型数据库
64MyBatis - bind元素
64MyBatis - bind元素
30 0
|
10月前
手写bind
手写bind
36 0
|
10月前
|
前端开发 JavaScript
面试官:你会手写bind apply 和bind吗?
面试官:你会手写bind apply 和bind吗?
|
前端开发 JavaScript
手写javascript中的call、apply、bind方法~
手写javascript中的call、apply、bind方法记录
47 1
手写系列 # 5:实现 bind 方法
手写系列 # 5:实现 bind 方法
63 0
手写系列 # 5:实现 bind 方法

热门文章

最新文章