call和apply原理实现

1.改变this的指向
2.让函数执行
3.多个call时会把call源码里的this改成要指向的

Function.prototype._call = function (context, ...arg) {
            context = context ? Object(context) : window;//将content包装成对象
            context.fn = this;//将原函数挂在content下,context.fn运行this就指向了content
            let result = context.fn(...arg)
            delete context.fn;
            return result;

        }
        Function.prototype._apply = function (context, arg) {
            context = context ? Object(context) : window;
            context.fn = this;
            if (!arg) {
                let result = context.fn()
                delete context.fn; 
                return result;
            }
            let result = context.fn(...arg)
            delete context.fn;
            return result;

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