var foo = {
value :1
}
function bar(name){
console.log('......')
console.log(name)
this.b = '22';
console.log(this.value)
}
// bar.bind(foo)()
//1.
// Function.prototype.bind2 = function(context){
// var self = this;
// return function(){
// self.apply(context)
// }
// }
//2.增加参数
// Function.prototype.bind2 = function(context){
// var self = this;
// var args = Array.prototype.slice.call(arguments,1)
// return function(){
// var argsBind = Array.prototype.slice.call(arguments)
// self.apply(context,args.concat(argsBind))
// }
// }
//3.构造函数
var value =3
bar.prototype.a = 'wgf'
Function.prototype.bind2 = function(context){
if (typeof this !== "function") {
throw new Error("Function.prototype.bind - what is trying to be bound is not callable");
}
var self = this;
var fnull = function(){}
var args = Array.prototype.slice.call(arguments,1)
var fn = function(){
var argsBind = Array.prototype.slice.call(arguments)
self.apply(this instanceof self?this :context,args.concat(argsBind))
}
fnull.prototype = this.prototype;
fn.prototype = new fnull();
return fn
}
var obj = bar.bind2(a);
// var obj1 = new obj('ccc');
// console.log(obj1.b)
console