手写call、apply和bind
Function.prototype.call = function (centext, ...args) {
if (typeof this !== 'function') {
return console.error('not a function')
}
centext = centext ? centext : window;
const fn = Symbol('fn');
centext[fn] = this;
const data = centext[fn](...args);
delete centext[fn];
return data;
}
Function.prototype.apply = function (centext, args) {
if (typeof this !== 'function') {
return console.error('not a function')
}
centext = centext ? centext : window;
const fn = Symbol('fn');
centext[fn] = this;
const data = centext[fn](...args);
delete centext[fn];
return data;
}
Function.prototype.bind = function (centext, args) {
if (typeof this !== 'function') {
return console.error('not a function')
};
const fn = Symbol('fn');
centext[fn] = this;
return function () {
const list = args.concat(...arguments);
centext[fn](...list);
delete centext[fn];
}
}
function fn (name, age) {
this.name = name;
this.age = age
}