Skip to content

Instantly share code, notes, and snippets.

@softwarespot
Last active April 21, 2017 19:06
Show Gist options
  • Save softwarespot/1d2d412a36f9edddd0a33fb8e9ae2c62 to your computer and use it in GitHub Desktop.
Save softwarespot/1d2d412a36f9edddd0a33fb8e9ae2c62 to your computer and use it in GitHub Desktop.
function DOM(selector) {
if (!(this instanceof DOM)) {
return new DOM(selector);
}
// Helper for pushing an array-like object onto the current instance
Array.prototype.push.apply(this, document.querySelectorAll(selector));
}
_inheritFrom(DOM, Array);
// Idea taken from babel when using the likes of "class Child extend Parent { ... }"
function _inheritFrom(child, parent) {
const parentProto = parent && parent.prototype ? parent.prototype : parent;
child.prototype = Object.create(parentProto, {
constuctor: {
value: parent,
configurable: true,
enumerable: false,
writable: true
}
});
Object.setPrototypeOf(child, parent);
}
// Example
// The IIFE is a way of being able to re-use const variables in the Chrome Snippets without reloading
(function () {
const $hyperLinks = DOM('a');
$hyperLinks.forEach(el => console.log(el));
const $navLinks = $hyperLinks.filter(el => el.classList.contains('header-nav-link'));
console.log($navLinks);
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment