Answer the question
In order to leave comments, you need to log in
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
Look at Function.prototype.bind This is the standard, there is a fallback for browsers without .bind support and traffic is minimal.
In jQuery - no, there is another useful Underscore library - _.bind
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question