Function.prototype.myBind = function (thisArg, ...boundArgs) {
if (typeof this !== 'function') {
throw new TypeError('myBind must be called on a function');
}
const targetFn = this;
function boundFn(...argArray) {
const isCalledWithNew = this instanceof boundFn;
const context = isCalledWithNew
? this
: thisArg === null || thisArg === undefined
? globalThis
: Object(thisArg);
return Reflect.apply(targetFn, context, [...boundArgs, ...argArray]);
}
if (targetFn.prototype) {
boundFn.prototype = Object.create(targetFn.prototype);
boundFn.prototype.constructor = boundFn;
}
return boundFn;
};