A
A
Alexander Wolf2012-08-11 19:27:07
JavaScript
Alexander Wolf, 2012-08-11 19:27:07

Is there an equivalent to .bind (MooTools) in JQuery?

Hello! Is there an analogue of .bind (from MooTools) in JQuery? Or will it have to be written by hand?

Solution: Write an extension to the Function prototype:

if (!Function.prototype.bind) {<br>
  Function.prototype.bind = function (oThis) {<br>
    if (typeof this !== "function") {<br>
      // closest thing possible to the ECMAScript 5 internal IsCallable function<br>
      throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable");<br>
    }<br>
 <br>
    var aArgs = Array.prototype.slice.call(arguments, 1), <br>
        fToBind = this, <br>
        fNOP = function () {},<br>
        fBound = function () {<br>
          return fToBind.apply(this instanceof fNOP && oThis<br>
                                 ? this<br>
                                 : oThis,<br>
                               aArgs.concat(Array.prototype.slice.call(arguments)));<br>
        };<br>
 <br>
    fNOP.prototype = this.prototype;<br>
    fBound.prototype = new fNOP();<br>
 <br>
    return fBound;<br>
  };<br>
}<br>

Answer the question

In order to leave comments, you need to log in

3 answer(s)
L
lacki, 2012-08-12
@mannaro

Look at Function.prototype.bind This is the standard, there is a fallback for browsers without .bind support and traffic is minimal.

G
gelas, 2012-08-11
@gelas

The closest, though not analogue, apparently jQuery.proxy

J
jQueryScripter, 2012-08-11
@jQueryScripter

In jQuery - no, there is another useful Underscore library - _.bind

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question