4 ms·
To just make the DOM API's a little cleaner is so simple in a language like JavaScript, why not just create the abstractions as you need them? This is not some
by jacobr 14y ago
To just make the DOM API's a little cleaner is so simple in a language like JavaScript, why not just create the abstractions as you need them?
This is not something you should use in production, but just a quick example:
function setAttr (attr, val) {
return function (elem) { elem.setAttribute(attr, val); };
}
NodeList.prototype.forEach = Array.prototype.forEach;
var $ = document.querySelectorAll.bind(document);
$('.foo').forEach(setAttr('foo', 'bar'));
- jdavis703 14y agoJQuery is slowly moving in that direction [0] and YUI has long let you pick and choose which "abstractions" you need [1]. [0] https://github.com/jquery/jquery/blob/master/README.md https://github.com/jquery/jquery/blob/master/README.md [1] http://yuilibrary.com/yui/configurator/ http://yuilibrary.com/yui/configurator/ [edited with better link to JQuery build documentation]
- masklinn 14y ago> This is not something you should use in production, but just a quick example: Your quick example is risky and likely broken to all MSIE <= 8: https://developer.mozilla.org/en-US/docs/DOM/NodeList#Why_cant_I_use_forEach_or_map_on_a_NodeList.3F https://developer.mozilla.org/en-US/docs/DOM/NodeList#Why_ca...
- mattmanser 14y agoThere's no forEach in IE8.