let dog = {
name:'teddy',
eat:function(arr1,arr2){
console.log(arg1+arg2)
console.log(this)
}
}
dog.eat.apply({a1:1,a2:2},[1,2])
dog.eat.call({a1:3,a2:4},1,2)
let newfn = dog.eat.bind({a1:4,a2:5})
newfn(6,7)
new 过程模拟
function A(a,b){
this.a = a
this.b = b
}
A.prototype.eat = function(){
console.log(this.a+"xxx")
}
function myNew(fn,...args){
let obj = {}
Object.setPrototypeOf(obj,fn.prototype)
fn.call(obj,...args)
return obj
}
let a = myNew(A,2,3)
a.eat()
类数组
let obj = {
0:"xx1",
1:"xx2",
2:"xx3",
length:3
}
Array.prototype.myFilter = function(callback){
let newArr = []
for(let i=0;i<this.length;i++){
if(callback(this(i))){
newArr.push(this[i])
}
}
return newArr
}
let res = [].myFilter.call(obj,item=>item.indexOf("1")!=-1)
console.log(res)