当前位置: 代码迷 >> 综合 >> 手写一些方法实现 大神勿喷
  详细解决方案

手写一些方法实现 大神勿喷

热度:73   发布时间:2024-01-28 21:25:23.0
// 手写简易版的es6中数组的includes方法
Array.prototype.includes = function (item, index) {var arr = this;var flag = false;if (!arguments[0] || !arguments[1]) return false;for (let i = 0; i < arr.length; i++) { if (!!index) {if ((typeof index == 'string' || typeof index == 'number') && (arr[i] == item && i == index)) { flag = true;break;}} else { if (arr[i] == item) { flag = true;break;}}}return flag;
}

第二篇


// 手写new函数的实现
function _new(Fn, ...args) {var obj = Object.create(Fn.prototype);// obj.__proto = Fn.prototype;var result = Fn.call(obj, ...args);return result != null && (typeof result == 'object' || typeof result == 'function') ? result : obj;
}

 

  相关解决方案