自己实现的一些js方法

  Array.prototype.filter = function(callback, thisArg) {
            debugger
            if (this === undefined) {
                throw new TypeError("this is null or not undefined");
            }
            if (typeof callback !== 'function') {
                throw new TypeError(callback + 'is not a function');
            }

            const res = [];

            const O = Object(this);
            const len = O.length >>> 0;
            for(var i = 0; i< len; i++) {

                if (i in O) {
                    if (callback.call(thisArg, O[i], i, O)) {
                        res.push(O[i]);
                    }
                }
            }

            return res;
        }

        const arr = [1, 2, 3, 4, 5, 6, 7]
        arr.filter(item => {
            return item > 3
        })

        Array.prototype.map  = function(callBack, thisArg) {
            debugger
            if (this === undefined) {
                throw new TypeError("this is null or not undefined")
            }

            if (typeof callBack !== 'function' ) {
                throw new TypeError(callback + 'is not a function');
            }

            const res = [];

            const O = Object(this);

            const len = O.length >>> 0;    

            for (let i = 0; i < len; i++) {
                if (i in O) {
                    res[i] = callBack.call(thisArg, O[i], i, this);
                }   
            }
            return res;
        }

        const arr = [1, 2, 3, 4, 5, 6, 7]
        arr.map(item => {
            console.log(item)
        })
 Array.prototype.forEach = function(callback, thisArg) {
            debugger
            if (this == null) {
                throw new TypeError('this is null or not defined');
            }
            if (typeof callback !== "function") {
                throw new TypeError(callback + ' is not a function');
            }
            const O = Object(this);
            const len = O.length >>> 0;
            let k = 0;
            while (k < len) {
                if (k in O) {
                callback.call(thisArg, O[k], k, O);
                }
                k++;
            }
        }
        const arr = [1, 2, 3, 4, 5, 6, 7]
        arr.forEach(item => {
            console.log(item)
        })
 Array.prototype.reduce = function(callback, initialValue) {
            debugger
            if (this == undefined) {
                    throw new TypeError('this is null or not defined');
                }
                if (typeof callback !== 'function') {
                    throw new TypeError(callbackfn + ' is not a function');
                }
                const O = Object(this);
                 const len = this.length >>> 0;
               
               let accumulator = initialValue;
               let k = 0;
                // 如果第二个参数为undefined的情况下
                // 则数组的第一个有效值作为累加器的初始值
               if (accumulator === undefined) {
                   while(k < len && !(k in O)) {
                       k ++;
                   }

                    // 如果超出数组界限还没有找到累加器的初始值,则TypeError
                    if (k >= len) {
                    throw new TypeError('Reduce of empty array with no initial value');
                    }
                    accumulator = O[k++];
               }

               while (k < len) {
                    if (k in O) {
                    accumulator = callback.call(undefined, accumulator, O[k], k, O);
                    }
                    k++;
                }

                return accumulator;
               
        }

        const arr = [9,4, 5, 6, 2, 8, 6]
        arr.reduce((item1, item2) => {
            console.log(item1, item2)
            return item1 + item2;
        })


  Function.prototype.apply = function(context = window, args) {

           if (typeof this !== 'function') {
               throw new TypeError('Type Error')
           }
           const fn = Symbol('fn')
           context[fn] = this;

           const res = context[fn](...args);
           delete context[fn];
           return res;
       }
Function.prototype.call = function(context = window, ...args) {
  if (typeof this !== 'function') {
    throw new TypeError('Type Error');
  }
  const fn = Symbol('fn');
  context[fn] = this;


  const res = context[fn](...args);
  delete context[fn];
  return res;
} 

你可能感兴趣的:(自己实现的一些js方法)