DEV Community

Ed Link III
Ed Link III

Posted on

Refactoring jQuery

A few months ago I posted Recovering From jQuery about learning Vanilla JavaScript. I have been working on that, but found some of the code a little cumbersome to try to refactor.

I ended up creating a little collection of helpers to simplify the code. Most of these will act as direct replacements for jQuery. Typically all that will be required is changing $() to $q().

This is not a total replacement for jQuery. However, it was enough to get rid of 95% of my jQuery dependence. I honestly prefer addClass() to classList.add().

js-query on GitHub

There are a lot of jQuery methods I could add (and probably will), but these are the bulk of what was being used in the project I'm working on now.

Please don't rake me over the coals too bad for the ajax() function, remember, I was trying to replace $.ajax().

Top comments (2)

Collapse
 
dexygen profile image
George Jempty

jQuery is not as evil as everybody thinks. It has some neat methods like "parentsUntil" IIRC. Also it has ajax built in which a lot of modern frameworks do not. Furthermore it is required by Bootstrap up until version 3. I've actually tinkered with adding reactivity to jQuery, though it's been over 2 years. Have other things on my plate right now, though I'm actually in contention for a jQuery role.

Collapse
 
edlinkiii profile image
Ed Link III

I have NOTHING against jQuery. I have the job now because of my knowledge of it. However, as I stated in the original post, I ONLY knew jQuery. In my learning true 'Vanilla' JavaScript, I found I favored the jQuery syntax (for readability and chain-ability).

(I've also been working on my own UI elements to get away from being dependent on jQueryUI.)

I'm working in Backbone, so completely getting rid of jQuery is NOT an option. I'm just tinkering and thought I'd share.