Skip to content

Instantly share code, notes, and snippets.

@zenparsing
Last active August 29, 2015 14:09
Show Gist options
  • Save zenparsing/611b8788ff8ffcfcc20e to your computer and use it in GitHub Desktop.
Save zenparsing/611b8788ff8ffcfcc20e to your computer and use it in GitHub Desktop.
jQuery Reimagined
/* Redefine jQuery methods as "raw" methods */
(function($) { "use strict";
function wrap(method) {
return function() {
var jq = $(this),
result = jq[method].apply(jq, arguments);
if (result && result.constructor === $)
result = result.toArray();
return result;
};
}
function wrapAll(out) {
var x = $();
for (var name in x)
if (typeof x[name] === "function")
out[name] = wrap(name);
return out;
}
wrapAll(window.jake = {});
})($);
// Create bindings for just the methods that we need
let { find, html } = jake;
// Find all the divs with class="myClass", then get all of the "p"s and replace their content.
document.querySelectorAll("div.myClass")::find("p")::html("hahaha");
// Notice that we didn't have to worry about wrapping/unwrapping elements and NodeLists.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment