call:
var foo ={
value:1
}
function bar(name,age) {
console.log(this.value);
console.log(name);
console.log(age);
}
//bar.call(foo,'gongtao',11);
Function.prototype.call2 = function (context) {
var args = [];
for (var i = 1, len = arguments.length; i < len; i++) {
args.push(arguments[i]);
}
context.fn = this;
context.fn(...args);
delete context.fn;
}
bar.call2(foo,'gongtao',111);
apply:
var foo = {
value: 1
}
function bar(name, age) {
console.log(this.value);
console.log(name);
console.log(age);
}
//bar.apply(foo);
Function.prototype.apply2 = function (context, arr) {
var context = context || window;
if (!arr) {
context.fn();
} else {
context.fn = this;
context.fn(...arr);
}
delete context.fn;
}
bar.apply2(foo, ["gongtao", 11]);
Bind
来源:oschina
链接:https://my.oschina.net/u/2285087/blog/3223410