bind 原理实现

    1.bind方法可以改变this的指向 绑定参数
    2.bind方法返回一个绑定this后函数
    3.如果绑定的函数被new了,当前函数的this就是当前实例
    4.new 出来的结果能够找到原有类的原型

        Function.prototype.bind = function (content) {
            let that = this;//保存this
            let bindArgs = Array.prototype.slice.call(arguments, 1);

            function fBind() {
                let args = Array.prototype.slice.call(arguments)
                return that.apply(this instanceof fBind ? this : content, bindArgs.concat(args))
            }
            function Fn(){};
            Fn.prototype = this.prototype;
            fBind.prototype = new Fn();

            return fBind;
        }
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。