自定义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

猜你喜欢

转载自www.cnblogs.com/Longhua-0/p/9419286.html