Function.prototype.mCall = function (obj, ...args) {
const nObj = typeof obj === 'undefined' ? globalThis : Object(obj);
const fn = this;
const key = Symbol();
nObj[key] = fn;
const result = nObj[key](...args);
delete nObj[key];
return result;
};
function foo(a, b) {
console.log(this);
console.log(a, b);
}
foo.mCall(1, 2, 3);
Function.prototype.mApply = function (obj, args) {
const nObj = typeof obj === 'undefined' ? globalThis : Object(obj);
const fn = this;
const key = Symbol();
nObj[key] = fn;
const result = nObj[key](...args);
delete nObj[key];
return result;
};
foo.mApply(1, [2, 3]);
Function.prototype.mBind = function (obj, ...args1) {
const nObj = typeof obj === 'undefined' ? globalThis : Object(obj);
const fn = this;
const key = Symbol();
nObj[key] = fn;
return function (...args2) {
const nArgs = [...args1, ...args2];
const result = nObj[key](...nArgs);
delete nObj[key];
return result;
};
};
const foo2 = foo.mBind(1, 2, 3);
foo2(4, 5);
02-10
1535
