From 090d8d9ce77f0f7b40293c343175c4b739899d1d Mon Sep 17 00:00:00 2001 From: Carl-Philip Majgaard <12858069+majgaard@users.noreply.github.com> Date: Mon, 14 Aug 2023 11:06:58 -0400 Subject: [PATCH] Use Object.defineProperty to extend prototypes --- src/extensions/Array.js | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/src/extensions/Array.js b/src/extensions/Array.js index bfe958c..de728d0 100644 --- a/src/extensions/Array.js +++ b/src/extensions/Array.js @@ -2,20 +2,33 @@ * Faster than .forEach * @param {(function())} fn The function to call */ -Array.prototype.each = function (fn) { +function arrayEach(fn) { const l = this.length - for(let i = 0; i < l; i++) { + for (let i = 0; i < l; i++) { const e = this[i] if (e) { - fn(e,i) + fn(e, i) } } } +Object.defineProperty(Array.prototype, "each", { + value: arrayEach, + enumerable: false, +}); + /** * Give NodeList some Array functions */ -NodeList.prototype.each = Array.prototype.each -NodeList.prototype.filter = Array.prototype.filter +Object.defineProperties(NodeList.prototype, { + each: { + value: Array.prototype.each, + enumerable: false, + }, + filter: { + value: Array.prototype.filter, + enumerable: false + } +}); \ No newline at end of file