Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS5 Doesn't Support bind()!

I have a client with an original iPad and what I noticed is that it doesn't support the .bind method.

Q: If my boss insists on supporting IOS 5.1.1, is there an alternative to passing variables to a callback? I don't think I can simply put the variable into the global scope because, if I'm in a loop, the variable that I set might overwrite the same variable that the callback is looking for.

like image 337
Phillip Senn Avatar asked Dec 02 '13 17:12

Phillip Senn


1 Answers

You can use the implementation provided at MDN, or even your own.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind#Compatibility

if (!Function.prototype.bind) {
  Function.prototype.bind = function (oThis) {
    if (typeof this !== "function") {
      // closest thing possible to the ECMAScript 5 internal IsCallable function
      throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable");
    }

    var aArgs = Array.prototype.slice.call(arguments, 1), 
        fToBind = this, 
        fNOP = function () {},
        fBound = function () {
          return fToBind.apply(this instanceof fNOP && oThis
                                 ? this
                                 : oThis,
                               aArgs.concat(Array.prototype.slice.call(arguments)));
        };

    fNOP.prototype = this.prototype;
    fBound.prototype = new fNOP();

    return fBound;
  };
}
like image 92
Tibos Avatar answered Sep 21 '22 05:09

Tibos