bind的使用

bind: 改变this的指向,返回一个新函数

let obj = {
name: 'jason888'
}

function fun(name,age){
//console.log(888);
//console.log("this:",this);
console.log("this.name:",this.name);
// console.log("arguments:",arguments.callee);
// console.log("Array:",Array.prototype);
// console.log(name);
// console.log(age);
}

//let foo = fun.bind(obj,'meay',33);
//foo();

//bind: 改变this指向,返回一个新函数
Function.prototype.bind2 = function(context){
let self = this;
return function(){
self.apply(context,Array.prototype.slice.call(arguments,1))
}
}

let foo = fun.bind2(obj,'meay',33);
foo();

猜你喜欢

转载自www.cnblogs.com/zhaodagang8/p/11032432.html