Last active
August 29, 2015 14:09
-
-
Save zenparsing/611b8788ff8ffcfcc20e to your computer and use it in GitHub Desktop.
jQuery Reimagined
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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 = {}); | |
})($); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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