Skip to content

Instantly share code, notes, and snippets.

@WebReflection
Created December 2, 2014 12:55
Show Gist options
  • Select an option

  • Save WebReflection/ed7b33dc345107689f8a to your computer and use it in GitHub Desktop.

Select an option

Save WebReflection/ed7b33dc345107689f8a to your computer and use it in GitHub Desktop.
Add Array.prototype methods to a generic constructor.prototype
function arrayFy(constructor) {
Object.getOwnPropertyNames(
Array.prototype
).forEach(function (key) {
var descriptor = Object.getOwnPropertyDescriptor(
Array.prototype,
key
);
if (
typeof descriptor.value === 'function' &&
!(key in constructor.prototype)
) {
Object.defineProperty(
constructor.prototype,
key,
descriptor
);
}
});
}
@WebReflection

Copy link
Copy Markdown
Author

example

arrayFy(NodeList);

document.querySelectorAll('*').forEach(function (node) {
  console.log(node.nodeName);
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment