let obj={
name:'我'
}
function fn(country,type){
console.log(this.name+'是'+country+type);
}
let newFn=fn.bind(obj,'中国');
newFn('人');
复制代码
打印结果:我是中国人.
bash
咱们能够得出结论:app
Function.prototype.mybind=function(context){
let fn=this;//先保存须要执行的函数
return function(){
fn.apply(context);//让原函数执行并经过apply改变this指向
}
}
复制代码
Function.prototype.mybind=function(context){
let fn=this;//先保存须要执行的函数
let binArgs=[].slice.call(arguments,1);//获取绑定this指向时的参数
return function(){
let reargs=[].slice.call(arguments);//获取调用绑定this后函数的参数
fn.apply(context,binArgs.concat(reargs));//让原函数执行并经过apply改变this指向,合并两次传递的参数
}
}
复制代码
另外,bind还有一个特色:调用完bind后返回的函数,还能够把这个函数当成一个类来调用. 用法:函数
let obj={
name:'我'
}
function fn(country,type){
this.name='你'
}
fn.prototype.city='北京'
let newFn=fn.bind(obj)
let oBind= new newFn();
console.log(oBind);
console.log(oBind.city);
复制代码
结果打印出: fn {name: "你"} 北京 咱们能够得出结论:ui
增长判断this
if(this instanceof fBound){
fn.apply(this,binArgs.concat(args));
}else{
fn.apply(context,binArgs.concat(args));
}
复制代码
并让返回的函数原型指向构造函数的原型spa
fBound.prototype=this.prototype;
复制代码
代码整合后:prototype
Function.prototype.mybind=function(context){
let fn=this;
let binArgs=[].slice.call(arguments,1)
function fBound(){
let reargs=[].slice.call(arguments)
if(this instanceof fBound){
fn.apply(this,binArgs.concat(reargs));
}else{
fn.apply(context,binArgs.concat(reargs));
}
}
fBound.prototype=this.prototype
return fBound
}
复制代码