Function.prototype.mybind = function (ctx) {
const args = Array.prototype.slice.call(arguments, 1)
const fn = this
return function A() {
const resultArgs = Array.prototype.slice.call(arguments)
const valArgs = args.concat(resultArgs)
// trigger by new
if (Object.getPrototypeOf(fn) === A.prototype) {
// 1
// return new fn(...valArgs)
// 2
/**
* 手写 new 运算符
* (1) 创建一个新对象
* (2) 将构造函数中的this指向该对象
* (3) 执行构造函数中的代码(为这个新对象添加属性
* (4) 返回新对象。
*/
const obj = {}
Object.setPrototypeOf(obj, fn.prototype)
const result = fn.apply(obj, valArgs)
// 如果执行结果有返回值并且是一个对象, 返回执行的结果, 否则, 返回新创建的对象
return typeof result === 'object' ? result : obj
}
else {
return fn.apply(ctx, valArgs)
}
}
}
function fn(a, b, c, d) {
console.log(this, a, b, c, d)
}
const newFun = fn.bind('ctx', 1, 2)
const result = new newFun(3, 4)
console.log(result)
console