手写 bind、call、apply
js
function myBind(ctx) {
ctx.fn = this
ctx.fn()
delete ctx.fn
}
手写 new
js
function myNew(fn, ...args) {
const obj = Object.create(fn.prototype)
const res = fn.apply(obj, args)
return res instanceof Object ? res : obj
}
手写 unshift
js
function myUnshift(...args) {
for (let i = args.length - 1; i >= 0; i--) {
this.splice(0, 0, args[i])
}
return this.length
}
实现管道函数
```js
const pipe = (...fns) => x => fns.reduce((v, fn) => fn(v), x);
const double = x => x * 2;
const square = x => x * x;
const squareDouble = pipe(double, square);
```