代码拉取完成,页面将自动刷新
同步操作将从 doocs/leetcode 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
增强所有函数,使其具有 callPolyfill
方法。该方法接受一个对象 obj
作为第一个参数,以及任意数量的附加参数。obj
成为函数的 this
上下文。附加参数将传递给该函数(即 callPolyfill
方法所属的函数)。
例如,如果有以下函数:
function tax(price, taxRate) { const totalCost = price * (1 + taxRate); console.log(`The cost of ${this.item} is ${totalCost}`); }
调用 tax(10, 0.1)
将输出 "The cost of undefined is 11"
。这是因为 this
上下文未定义。
然而,调用 tax.callPolyfill({item: "salad"}, 10, 0.1)
将输出 "The cost of salad is 11"
。this
上下文被正确设置,函数输出了适当的结果。
请在不使用内置的 Function.call
方法的情况下解决这个问题。
示例 1:
callPolyfill
示例 2:
callPolyfill
提示:
typeof args[0] == 'object' and args[0] != null
1 <= args.length <= 100
2 <= JSON.stringify(args[0]).length <= 105
declare global {
interface Function {
callPolyfill(context: Record<any, any>, ...args: any[]): any;
}
}
Function.prototype.callPolyfill = function (context, ...args): any {
const fn = this.bind(context);
return fn(...args);
};
/**
* function increment() { this.count++; return this.count; }
* increment.callPolyfill({count: 1}); // 2
*/
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。