0

I use a web framework. and this framework define extension for any array in my application

if (typeof Array.prototype.insertAt !== "function") {
    Array.prototype.insertAt = function (index) {
        this.splice.apply(this, [index, 0].concat(
            Array.prototype.slice.call(arguments, 1)));
        return this;
    };
}

I have an array in my application looks like this (it inherit insertAt and peek from web framework)

enter image description here

and my application use 3rd party library.. and this library using for in loop to iterate my array.. since my array has property insertAt this will cause break the library functionality.

Can any one help me how is the BEST way to solve this problem?

  1. Change the array prototype in web framework to make it not iterable
  2. Change the 3rd party library to use other loop instead of for in loop
  3. Create the bridge in my appplication to make only array member iterable
  4. Other solution?
wapt49
  • 213
  • 2
  • 16
  • 1
    Mutating built-in prototypes is a [horrible idea](https://stackoverflow.com/q/14034180). It's not only inelegant, it can damage the future evolution of the language if enough sites do it. (This is why we have `Array.prototype.includes` instead of `Array.prototype.contains`, and `Array.prototype.flat` instead of `Array.prototype.flatten`). I'd strongly recommend using a different framework that doesn't follow such bad practices. – CertainPerformance May 20 '20 at 18:32
  • 1
    All of 1, 2, and 4 (don't add custom methods to arrays at all). – Bergi May 20 '20 at 18:38

0 Answers0