3 ms·
I was playing a bit with this code last night and made a slight variation for some code I was writing, which I find easier to use //K variation jQuery.fn.r
by lhorie 16y ago
I was playing a bit with this code last night and made a slight variation for some code I was writing, which I find easier to use
//K variation
jQuery.fn.run = function(fn) {
var args = [].slice.call(arguments)
args[0] = this
fn.apply(this, args)
return this
}
//contrived example
var add = function(el, a, b) {console.log(a + b)}
//no need to curry
$(".foo").run(add, 12, 34).addClass("bar")
- raganwald 16y agoYour wish is a contributor's command: http://github.com/raganwald/JQuery-Combinators/commit/728713ee8bb1477d3ba383b7f63361298217bb3c http://github.com/raganwald/JQuery-Combinators/commit/728713... I avoid passing a function and its arguments in the same list but it's purely a personal preference and adding the code doesn't remove any existing functionality, so it's in.
- lhorie 16y agoYeah, looking in retrospect, binding the this keyword to the function already exposes the jQuery object inside the function, so the first argument in my version was redundant.