导语
手写bind
编辑
代码部分
Function.prototype.mybind = function (target) { //target:改变返回函数执行时的this指向 var obj = target || window; console.log(arguments,"arguments") var args = [].slice.call(arguments, 1); //获取bind时传入的绑定实参 var args1 = [].slice.call(arguments, 0); //获取bind时传入的绑定实参 console.log(args,"args") console.log(args1,"args1") var self = this; //要bind的函数 console.log(self,"self") var _fn = function () { var _args = [].slice.call(arguments, 0); //新函数执行时传递的实际参数 console.log(_args,"_args") return self.apply(obj, args.concat(_args)); } return _fn } var name = 'fangfang'; var obj = { name: 'geyao' }; function fn(a, b, c) { console.log(a + b + c + this.name); return a + b + c + this.name }; fn.mybind(obj,"我的","老婆","是")(); // 我的老婆是geyao