Function. prototype. mybind = function ( target, ... args) {
if ( typeof this !== 'function' ) throw new Error ( 'type error' )
let _this = this
let tmp = function ( ) { } ;
let f = function ( ... args2) {
return _this. call ( this instanceof tmp ? _this : ( target || window) , ... args, ... args2)
}
tmp. prototype = _this. prototype
f. prototype = new tmp ( ) ;
return f
}
function show ( ) {
console. log ( this , arguments)
}
let obj = {
name: 'obj'
}
let fun = show. mybind ( obj, 'init' )
fun ( 'end' )
let nfun = new fun ( )
console. log ( nfun, nfun. constructor)
Function. prototype. myBind = function ( target= window, ... args) {
if ( typeof this !== 'function' ) throw new Error ( 'type error' )
target. fun = this
let tmp = function ( ) { }
const f = function ( ... args2) {
const val = target. fun ( ... args, ... args2)
delete target. fun
return val
}
tmp. prototype = target. fun. prototype
f. prototype = new tmp ( )
return f
}