function curry(fn) {
return function curried() {
const args = Array.from(arguments)
if(args.length>=fn.length) {
return fn.apply(this, args)
}else {
return function() {
return curried.apply(this, args.concat(Array.from(arguments)))
}
}
}
}
function add(a,b,c) {
return a+b+c
}
const curriedAdd = curry(add)
console.log(curriedAdd(1)(2)(3))
Function.prototype.myBind = function() {
const args = Array.from(arguments)
const thisArg = args.shift();
const self = this;
return function() {
return self.apply(thisArg, args)
}
}
function fn1(a, b, c) {
console.log('this', this)
console.log(a, b, c)
return 'this is fn1'
}
const fn2 = fn1.myBind({x: 100}, 10, 20, 30)
const res = fn2()
console.log(res)
function myNew(obj, ...rest) {
const newObj = Object.create(obj)
const result = obj.apply(newObj, rest)
return typeof result === 'object' ? result : newObj;
}
function Person(firtName, lastName) {
this.firtName = firtName;
this.lastName = lastName;
}
const tb = new Person('Chen', 'Tianbao');
console.log(tb);
const tb2 = myNew(Person, 'Chen', 'Tianbao');
console.log(tb2)
console