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



目录
相关文章
|
5月前
|
C++
【C++】bind绑定包装器全解(代码演示,例题演示)
【C++】bind绑定包装器全解(代码演示,例题演示)
|
5天前
|
存储 算法 对象存储
【C++入门到精通】function包装器 | bind() 函数 C++11 [ C++入门 ]
【C++入门到精通】function包装器 | bind() 函数 C++11 [ C++入门 ]
14 1
|
9月前
|
前端开发 Shell
前端笔记之手写bind
前端笔记之手写bind
35 0
|
4月前
|
资源调度 JavaScript 前端开发
js高级进价 - 手写bind
js高级进价 - 手写bind
|
6月前
|
JavaScript 前端开发
Vue系列教程(08)- 基本语法(v-bind、v-if、v-for)
Vue系列教程(08)- 基本语法(v-bind、v-if、v-for)
43 0
|
9月前
手写bind
手写bind
36 0
|
9月前
|
前端开发 JavaScript
面试官:你会手写bind apply 和bind吗?
面试官:你会手写bind apply 和bind吗?
|
前端开发
前端学习案例2-bind的重写
前端学习案例2-bind的重写
30 0
前端学习案例2-bind的重写
|
前端开发
前端学习案例1-bind的重写
前端学习案例1-bind的重写
56 0
前端学习案例1-bind的重写
手写系列 # 5:实现 bind 方法
手写系列 # 5:实现 bind 方法
61 0
手写系列 # 5:实现 bind 方法