Function.prototype.myCall = function (context) {
// 首先判断调用对象
if (typeof this !== 'function') {
console.log('type error')
}
// 获取参数
let args = [...arguments].slice(1); //第一个是对象,从第二个开始
result = null
// 判断content(this指针要指向的对象)是否传入, 如果没有设置为window
context = context || window
// 将调用函数设置为对象的方法
context.fn = this
// 调用函数
result = context.fn(...args);
// 删除属性
delete context.fn
return result
}
const Test=function(){
console.log(this,'当前this的值',arguments)
}
Test.myCall({name:'ccc'},12);
Test.call({name:'999'},90);
js手写 call
最新推荐文章于 2024-08-02 14:21:03 发布