Function.prototype.bind2 = function (context) {
if (typeof this !== "function") {
throw new Error("Function.prototype.bind - what is trying to be bound is not callable");
}
const _this = this
const args = Array.prototype.slice.call(arguments, 1)
var fNOP = function () { }
var fBound = function () {
var bindArgs = Array.prototype.slice.call(arguments)
return _this.apply(this instanceof fNOP ? this : context, args.concat(bindArgs))
}
console.log(_this)
console.log(this)
fNOP.prototype = this.prototype;
fBound.prototype = new fNOP()
return fBound
}
function b(age, work, were) {
console.log(age)
console.log(work)
console.log(were)
console.log(this.name)
}
const obj = {
name: 'kalen'
}
let bin = b.bind2(obj, '18', 'fe2')
bin('fe')
console.log('=================================')
function b2(age, work, were) {
console.log(age)
console.log(work)
console.log(were)
console.log(this.name)
}
const obj2 = {
name: 'kalen2'
}
let bin2 = b2.bind2(obj, '28', 'fe = 2')
new bin2('fe new')
console.log('=================================')
let bin3 = b2.bind(obj, '38', 'f3', 'f3333')
new bin3('fffffff')
console