Function.prototype.myCall=function (context,args) {
console.log(arguments)
//context 表示call里面的第一个参数也就是需要改变this指向的那个对象。
//this表示这个方法
//把这个方法挂到需要改变指向的对象身上调用,相当于把this指向了这个对象身上,从而达到改变this的作用。
//argument是一个特殊的对象,可以获取方法传入的参数。
context.fn=this
let arr = []
for(let i =1;i<arguments.length;i++){
console.log(arguments[i])
//arr.push(arguments[i])
arr.push('arguments[' + i + ']');
}
console.log(arr)
console.log(arguments[1])
eval('context.fn('+arr+')')
//context.fn();
delete context.fn
}
let person = {
name:'tom',
age:22,
sex:'male',
sayName:function(){
console.log(this.name)
},
showInfo:function(name,age,sex){
this.name=name;
this.age=age;
this.sex=sex;
console.log('name:'+this.name+',age:'+this.age+',sex:'+this.sex)
}
}
let person2={
name:'tony'
}
person.showInfo('lily',22,'female')
person.showInfo.myCall(person2,'john',11,'male') //name:john,age:11,sex:male
文章来源地址https://uudwc.com/A/Pm32z
文章来源:https://uudwc.com/A/Pm32z