2

If I want to add a prototype to JavaScript's Array, is there a way to export it so that I can put all of the prototype methods in a file such as modules/prototypes.js

Would it be export Array? or would I add export prior to setting the prototype?

SumNeuron
  • 4,850
  • 5
  • 39
  • 107
  • Modifiying the prototypal inheritance behavior of the built in `Array` is a really bad idea as it can interfere with so many operations further down the line. – DMcCallum83 May 13 '18 at 10:03
  • @DMcCallum83 understood; nonetheless any idea how one _would_ do this? – SumNeuron May 13 '18 at 11:38
  • I assume you don't mean "add a prototype", but rather "add a method to the prototype". –  May 13 '18 at 12:14

1 Answers1

6

If your code modifies the global Array.prototype object (hint: it should not), you don't need to export anything. You just need to run it. You'd include the module that does this with

import "prototype/array.js";

but not get any particular imports. All arrays would simply inherit from the (now modified) global Array.prototype.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • thank you for answering the question asked. I appreciate it, was just confused to the syntax as to how one _would_ do this, not that one should. – SumNeuron May 13 '18 at 12:11
  • 1
    Not "all arrays you create", but "all arrays created in the past, present or future by you or anyone else (in this realm)". –  May 13 '18 at 12:13