// 1、函数A调用bind方法时,需要传递参数
// 2、返回一个新的函数B
// 3、函数B在执行的时候,还是使用A的功能,但是this指向改变了
// 4、函数B在执行的时候,传递的参数会拼接到bind方法的参数后面,然后一起在内部传递给A执行
// 5、new B()的构造函数仍然是A,改变this指向不会起任何作用
Function.prototype.myBind = function (target) {
var self = this;
var args = [].slice.call(arguments, 1);
var temp = function () { };
var f = function () {
var _args = args.concat([].slice.call(arguments, 0));
var _target = this instanceof temp ? self : (target || window);
return self.apply(_target, _args);
}
temp.prototype = self.prototype;
f.prototype = new temp();
return f;
}
function show(a, b, c, d) {
console.log(this, a, b, c, d);
}
var obj = {
a: 1
}
var newShow = show.myBind(obj, "a", "b", "c");
newShow("d");