Function.prototype.call2 = function (context) {
context = context || window;
context.fn = this;
let args = Array.prototype.slice.call(arguments, 1);
context.fn(...args);
delete context.fn;
}
Function.prototype.bind2 = function (context) {
self = this;
let args = Array.prototype.slice.call(arguments, 1);
let NullFn = function () {}
let Fb = function () {
let bindArgs = Array.prototype.slice.call(arguments);
return self.apply(this instanceof Fb ? this : context, args.concat(bindArgs));
};
NullFn.prototype = self.prototype;
Fb.prototype = new NullFn();
return Fb;
}
var obj = {
name: 'DreamFox',
age: 28
};
function sayA(name) {
console.log("I'm %s, %d years old", name, this.age);
}
sayA();
sayA.call2(obj, 'wangxin');
var value = 2;
var foo = {
value: 1
};
function bar(name, age) {
this.habit = 'shopping';
console.log(this.value);
console.log(name);
console.log(age);
}
bar.prototype.friend = 'kevin';
var bindFoo = bar.bind2(foo, 'daisy');
var obj = new bindFoo('18');
console