Function.prototype.myApply = function (context) {
if (typeof this !== 'function') throw new TypeError("Error");
let result = null;
context = context || window;
context.fn = this;
if (arguments[1]) {
result = context.fn(...arguments[1])
} else {
result = context.fn()
}
delete context.fn;
return result;
}
Function.prototype.myCall = function (context) {
if (typeof this !== 'function') throw new TypeError("Error");
let result = null;
context = context || window;
context.fn = this;
const args = [...arguments].slice(1);
result = args.length
? context.fn(...args)
: context.fn()
delete context.fn;
return result
}
Function.prototype.myBind = function (context) {
if (typeof this !== 'function') throw new TypeError("Error");
const args = [...arguments].slice(1);
context = context || window;
const fn = this;
return function Fn() {
return fn.apply(
this instanceof Fn
? this
: context,
args.concat(...arguments)
)
}
}
function newObj(fn) {
const obj = Object.create(fn.prototype);
const result = fn.apply(obj, [...arguments].slice(1));
return typeof result === 'object' ? result : obj
}
function ts(a, b) {
console.log(this.age)
console.log(a)
console.log(b)
return 'cc'
}
const b = ts.myBind({age: 666}, 'a')
b('b')
function Person(name, age) {
this.name = name;
this.age = age;
}
console