實現一個bind
Function.prototype.bind1 = function (context) {
const args = Array.prototype.slice.call(arguments, 1);
const self = this;
console.log(this, args, arguments)
return function () {
const innerArgs = Array.prototype.slice.call(arguments);
const finalArgs = args.concat(innerArgs);
return self.apply(context, finalArgs);
}
}
// test
const a = { 's': 3 }
function co (x) {
console.log(x, this)
}
co.bind1(a, '----')()
唯一不變的,恐怕只有變化本身了吧~