2019-04-12 你不知道的Javascript之bind实
2019-04-12 本文已影响0人
esbook
```javascript
var key = "window test"
Function.prototype.newBind = function(target){
var self = this;
var args = [].slice.call(arguments,1)
var temp = function(){}
var f = function(){
var _args = [].slice.call(arguments,0)
return self.apply(this instanceof temp ?this:(target||window),args.concat(_args))
}
temp.prototype = self.prototype
f.prototype = new temp()
return f;
}
function show(w,x,y,z){
console.log(this.key)
console.log(w,x,y,z)
}
var obj = {
key:'obj test'
}
var newShow = show.newBind(obj,1,2,3);
newShow(4)
var _new = new newShow();
console.log(_new.constructor)
```