Function.prototype.call2 = function (...context) {
//获取需要指向的对象
var ctx = context[0] || window;
//获取剩余参数
var args = context.slice(1);
//获取待改变指向的函数,并为需要指向的函数赋值待改变指向的函数
ctx.fn = this;
const result = ctx.fn(...args)
delete ctx.fn
return result
};
var obj = {
value: 1
}
function bar(name, age) {
return {
value: this.value,
name: name,
age: age
}
}
console.log(bar.call2(obj, 'lizn', 18))
JS 中Call函数的实现
于 2022-07-27 17:28:01 首次发布