function_new(fn, ...args) { //基于构造函数原型创建一个新对象 let obj = Object.create(fn.prototype);
// 添加属性到新创建的newObj上, 并获取obj函数执行的结果. let res = fn.apply(obj, args);
// 如果执行结果有返回值并且是一个对象, 返回执行的结果, 否则, 返回新创建的对象 returntypeof res === "object" ? res : obj; }
实现 call
call() 方法使用一个指定的 this 值和单独给出的一个或多个参数来调用一个函数。
语法:
1
function.call(thisArg, arg1, arg2, ...)
参数: thisArg 可选的。在 function 函数运行时使用的 this 值。请注意,this 可能不是该方法看到的实际值:如果这个函数处于非严格模式下,则指定为 null 或 undefined 时会自动替换为指向全局对象,原始值会被包装。**arg1, arg2,…**指定的参数列表。
返回值: 使用调用者提供的 this 值和参数调用该函数的返回值。若该方法没有返回值,则返回 undefined。
1 2 3 4 5 6 7 8 9 10 11 12 13 14
Function.prototype.mycall = function (context, ...args) { if (typeof context === "undefined" || context === null) { context = window; } let fnKey = Symbol();
context[fnKey] = this;
let result = context[fnKey](...args);
delete context[fnKey];
return result; };
实现 apply
apply() 方法调用一个具有给定 this 值的函数,以及以一个数组(或类数组对象)的形式提供的参数。和 call 类似,只是第二个参数不同
1 2 3 4 5 6 7 8 9 10 11 12 13 14
Function.prototype.myapply = function (context, args) { if (typeof context === "undefined" || context === null) { context = window; } let fnKey = Symbol();
context[fnKey] = this;
let result = context[fnKey](...args);
delete context[fnKey];
return result; };
实现 bind
bind() 方法创建一个新的函数,在 bind() 被调用时,这个新函数的 this 被指定为 bind() 的第一个参数,而其余参数将作为新函数的参数,供调用时使用。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Function.prototype.mybind = function (allArgs) { if (typeofthis !== "function") { thrownewError("bind必须绑定在函数上"); } let self = this; //保存需要调用的函数 let args = Array.prototype.slice.call(arguments, 1);
functionbindfn(...argArr) { var _this = thisinstanceof bindfn ? this : allArgs; //是否是用new调用,如果是new,此时绑定函数中的 this 应是由 new 调用绑定函数返回的实例对象 self.apply(_this, args.concat(argArr)); }
Promise.myall = function (promises) { if (!Array.isArray(promises)) { return; } let len = promises.length; let result = newArray(len); let count = 0;
returnnewPromise((resolve, reject) => { for (let i = len - 1; i >= 0; i--) { Promise.resolve(promises[i]) .then((data) => { result[i] = data; if (++count === len) { resolve(result); } }) .catch((e) => reject(e)); } }); };
实现 Array.prototype.reduce
1 2 3 4 5 6 7 8 9
Array.prototype.myReduce = function (callback, initialValue) { let flag = initialValue === undefined ? false : true; let value = flag ? initialValue : this[0]; let i = flag ? 0 : 1; for (let j = i; j < this.length; j++) { value = callback(value, this[j], i, this); } return value; };