首页 > 编程知识 正文

excel函数公式大全讲解(bind实现原理)

时间:2023-05-03 08:15:21 阅读:74783 作者:2596

以前写了call和apply的具体实现模拟,今天试着写一下bind函数的具体实现

bind :用一句话说明,会创建新函数。 调用此函数时,传递的第一个参数将用作新的this对象,后续参数将用作绑定函数的参数。 新函数具有与被调整函数(绑定函数的目标函数)相同的函数主体。

与call和apply相同的点:都可以传递参数

区别在于,apply和call具体执行回调函数,而bind返回函数

补充说明:绑定后不能再次使用call和apply修改this指向

首先,实现简单的绑定

functionbindfoo({console.log ) ' mynameis'this.name}; } varbindobj={ name : ' GUI ' } var foo=bind foo.bind (bind obj ); 改变this的方向,返回新函数foo (; //函数执行输出My name is gui所以从上面的代码可以看出,最重要的是改变this指向,如何改变可以考虑以前分析的call和apply来实现

所以,用call和apply的方式,可以很容易地改写bind函数

function.prototype.bind func=function (context ) {var _self=this; //_self是bindFoo函数//_ self=function bindfoo ({ console.log (myna meis ' this.name ) ); }在}return function () apply中使用this对象_self.apply(context ); }在步骤2中,考虑与call、apply一致的参数情况

functionbindfoo(age,height,weight )控制台. log (myna meis ' this.name )。 我是age is ' age '。 我是height is ' height '。 微信is '微信; }var bindObj={name : 'gui'}; function.prototype.bind func=function (context ) {var _self=this; //点1,因为对象是先从第一个arguments获取并分割,然后在varfoo=bindfoo.bind(bindobj,' 18 ' )中首先传递。 因此,获取从第二个到最后一个参数varargs=array.prototype.slice.call (arguments,1 )。 return function ()//重点2,在此获取的arguments是第二个foo('175 )、' 130 )传递的参数,因此不需要从第二个获取)//,而是调用、应用和_ self.apply (上下文,bindArgs ); } var foo=bind foo.bind func (bind obj,' 18 ' ); 改变this的方向,返回新函数foo('175 )、' 130 ); //函数执行输出My name is gui。 age is 18。 海特Is 175。 weight is 130为var foo=bind foo.bind func (bind obj,‘18’除外; foo(‘175”、“130”)的名称还有一个new对象的名称。

new对象的方式是将返回的函数作为新的构造函数,重新构建对象,传递原始参数,由此生成上述var foo=bind foo.bind func (bind obj,‘18’);

我们将varfooobj=new foo (‘175’、‘130’); 如所示传递新参数。 在这种情况下,fooObj作为新对象,在new的情况下也相应地执行bindFoo方法。

举个例子:

functionbindfoo(age,height,weight )控制台. log (myna meis ' this.name )。 我是age is ' age '。 我是height is ' height '。 微信is '微信; } varbindobj={ name : ' GUI ' } varname=' sichao '; varfoo=bindfoo.bind(bindobj,' 18 ' ); 改变this的方向,返回新函数varfooobj=newfoo('175 ',' 130 ' ); 输出我的名字is undefined。 age is 18。 海特Is 175。 weight is 130侧重于My name is undefined,其中name输出到undefined,实际上在新对象时,bindFoo的this已经指向了fooObj,而fooObj则没有

所以,针对这种情况,我再写一版:

function.prototype.bind func=function (context ) {var _self=this; varargs=array.prototype.slice.call (arguments,1 ); var bindFunc=function () varargs2=array.prototype.slice.call (arguments,0 ); varbindargs=args.concat(args2); /*步骤2 :对于构造函数newfoo('175 )、' 130 ),this是指实例。 因为在第一步中bind func.prototype=this.prototype,所以如果结果为true,如果结果为true,则为this。对于常规函数foo('175 ',' 130 ' ),this将在winis */varbindthis=thisinstanceofbindfunc? this :上下文; _self.apply(bindthis,bindArgs ); //第一步:如果第一次将返回函数bindFunc原型对象更改为绑定函数bindFoo的原型对象//实例,则绑定函数原型的值bind func.prototype= 返回绑定函数; }这样写又有问题。 这意味着修改bindFunc.prototype会直接影响绑定函数的prototype

因此,必须用空函数中继。 使用了原型链继承方式。

function.prototype.bind func=function (context ) {var _self=this; varargs=array.prototype.slice.call (arguments,1 ); var turnFunc=function () }; var bindFunc=function () varargs2=array.prototype.slice.call (arguments,0 ); varbindargs=args.concat(args2); varbindthis=thisinstanceofbindfunc? this :上下文; _self.apply(bindthis,bindArgs ); } turn func.prototype=_ self.prototype; bindFunc.prototype=new turnFunc (; 返回绑定函数; }最后,让我们与绑定函数不正确的情况兼容:

function.prototype.bind func=function (context ) if ) typeofthis!=' function ' (console.log (type of this ) ); thrownewerror('notfunction!' ); }var _self=this; varargs=array.prototype.slice.call (arguments,1 ); var turnFunc=function () }; var bindFunc=function () varargs2=array.prototype.slice.call (arguments,0 ); varbindargs=args.concat(args2); varbindthis=thisinstanceofbindfunc? this :上下文; _self.apply(bindthis,bindArgs ); } turn func.prototype=_ self.prototype; bindFunc.prototype=new turnFunc (; 返回绑定函数; }

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。