Javascript - IE8替代bind() [英] Javascript - IE8 alternative to bind()

查看:326
本文介绍了Javascript - IE8替代bind()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

img.attachEvent("onclick", function(img){

 alert(img.id);

}.bind(null,img)); 

我如何在IE 8中完成这项工作?或者最直接的选择是什么?

How can i make this work in IE 8? Or what is the most direct alternative?

推荐答案

我找到了一个polyfill 这里

I found a polyfill here.

Function.prototype.bind = Function.prototype.bind || function(b) {
    if (typeof this !== "function") {
      throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable");
    }

    var a = Array.prototype.slice;
    var f = a.call(arguments, 1);
    var e = this;
    var c = function() {};
    var d = function() {
      return e.apply(this instanceof c ? this : b || window, f.concat(a.call(arguments)));
    };

    c.prototype = this.prototype;
    d.prototype = new c();

    return d;
};

这篇关于Javascript - IE8替代bind()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆