JS 实现 bind 方法
题干
- bind
题解
js
Function.prototype.myBind = function (context) {
if (typeof this !== 'function') {
throw new TypeError('Error')
}
let _this = this
let arg = [...arguments].slice(1)
return function F() {
// 处理函数使用new的情况
if (this instanceof F) {
return new _this(...arg, ...arguments)
} else {
return _this.apply(context, arg.concat(...arguments))
}
}
}
相关
如果一个构造函数 bind 一个对象,用此构造函数创建出的实例会继承这个对象的属性吗?为什么?