call和apply重写

经常能使用到call和apply的情况下进行内部实现

<script>
            //ES6版
            Function.prototype.newCall = function (obj) {
                //判断参数传入的值是否为null或者undefined
                obj = obj ? Object(obj) : window;
                //this指向设置为调用对象
                obj.func = this;  
                //截取参数数组中除了元对象外的参数列表
                let args = [...arguments].slice(1);
                //当前保存有this指向和参数内容的变量
                let res = obj.func(...args);
                //删除该函数
                delete obj.func
                //返回上下文
                return res;
            }
            //ES5
            Function.prototype.newCall = function (obj) {
                obj = obj ? Object(obj) : window; 
                obj.func = this;
                var args = [];
                //为取代扩展运算符使用手动分割数组为参数列表
                for (var i = 1, len = arguments.length; i < len; i++) {
                    args.push('arguments[' + i + ']');
                }
                //args 会自动调用 args.toString() 方法
                var result = eval('context.func(' + args + ')');
                delete obj.func
                return result; 
            }

            //ES6版
            Function.prototype.newApply = function (context, arr) {
                //判断参数传入的值是否为null或者undefined
                context = context ? Object(context) : window;
                //this指向设置为调用对象
                context.fn = this;
                //当前保存有this指向和参数内容的变量
                let result;
                // 判断是否存在第二个参数
                if (!arr) {
                    result = context.fn();
                } else {
                    //数组参数合并为一个参数列表
                    result = context.fn(...arr);
                }
                //删除该函数
                delete context.fn
                //返回上下文
                return result;
            }

            //ES5
            Function.prototype.newApply = function (context, arr) {
                context = context ? Object(context) : window;
                context.fn = this;
                var result;
                // 判断是否存在第二个参数
                if (!arr) {
                    result = context.fn();
                } else {
                    //更改传参方法
                    var args = [];
                    for (var i = 0, len = arr.length; i < len; i++) {
                        args.push('arr[' + i + ']');
                    }
                    result = eval('context.fn(' + args + ')');
                }
                delete context.fn
                return result;
            }
        </script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值