自定义bind方法

Posted longhua-0

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了自定义bind方法相关的知识,希望对你有一定的参考价值。

附:IE6-8自定义bind方法(IE6-8不兼容bind)

原理:

通过对Function的prototype原型进行扩展,可以为IE6~8自定义bind方法。

代码如下:

if (!function() {}.bind) { Function.prototype.bind = function(context) { var self = this , args = Array.prototype.slice.call(arguments); return function() { return self.apply(context, args.slice(1)); } };}

1、用call为函数的参数绑定splice方法;

2、给Function函数的this改变成bind传入的this对象;

3、把参数从第二个计算(第一个是传入的this对象);

 

链接:http://m.look.360.cn/transcoding?sign=360_e39369d1&url=9b0ce3f4873e7791b

以上是关于自定义bind方法的主要内容,如果未能解决你的问题,请参考以下文章