call+apply

99 阅读1分钟
        Function.prototype.mycall = function(myobj = window, ...args) {
            if(typeof myobj === "function") {
                throw new TypeError("error");
            }
            const fn = Symbol("fn"); //unique
            myobj[fn] = this;
            const result = myobj[fn](...args);
            delete myobj[fn];
            return result;
        }

        Function.prototype.myapply = function(myobj = window, args = []) {
            if(typeof myobj === "function") {
                throw new TypeError("error");
            }
            const fn = Symbol("fn"); //unique
            myobj[fn] = this;
            const result = myobj[fn](...args);
            delete myobj[fn];
            return result;
        }

        Function.prototype.mybind = function(myobj, ...args) {
            if(typeof myobj === "function") {
                throw new TypeError("error");
            }

            let self = this;
            let bound = function() {
                // 检测 New , 如果当前函数的this指向的是构造函数中的this 则判定为new 操作
                let _this = this instanceof self ? this : myobj;
                self.apply(_this, [...args, ...arguments]);
            };
            return bound;
        }