5 ms·
JQuery Combinators: Make any function into a jQuery method
- dugmartin 16y agoVery nice. Although I think the documentation would be better served by first showing the code. Its very short and very self explanatory. It's double awesome that it hooks into Functional Javascript. Here is the code: ;(function ($) { $.fn.K = function (fn) { fn = typeof(Functional) != 'undefined' ? Functional.lambda(fn) : fn; fn(this); return this; }; if (typeof($.fn.tap) == 'undefined') $.fn.tap = $.fn.K; $.fn.T = function (fn) { fn = typeof(Functional) != 'undefined' ? Functional.lambda(fn) : fn; return fn(this); }; if (typeof($.fn.into) == 'undefined') $.fn.into = $.fn.T; })(jQuery);
- d0m 16y agoI think he should use simpler and more direct examples. I mean, Go only make the example more complicated with the intersection and stuff. It's like if I'm trying to show how filter works by using a 20 lines example of a game.. why not use a simple <p> and show what you can do with it in a simple one liner? Ok, I know why, since it's functional stuff and more than that, COMBINATOR STUFF, it needs to be more complicated than what it should be.
- raganwald 16y agoOr it could be that those are examples from his own code and he simply copied and pasted them without thinking that they might not be as useful as contriving more simple examples. Do you have some examples you can suggest?
- d0m 16y agoActually, yes. At the end of his "tutorial", he explains in more succinct words what those functions do, and he use simpler examples. I suggest that instead of hiding them somewhere at the end in the middle of a paragraph, he put them in emphasis at the begin of the document.
- alnayyir 16y agoYou're talking to the author.
- jamesbritt 16y agoShhhh! This is good. :)
- raganwald 16y agohttp://github.com/raganwald/JQuery-Combinators/issues/issue/3 http://github.com/raganwald/JQuery-Combinators/issues/issue/...
- lhorie 16y agoI 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.