Array.prototype.slice web-compat issue?

Andrea Giammarchi andrea.giammarchi at gmail.com
Tue Sep 10 18:03:47 PDT 2013


Allen if you put the word `Array` but you define it differently then maybe
it's not me misunderstanding ... Float32Array is not an Array in current
ES, that's "fun enough" if you ask me.

In any case, if that was the problem, being an Array, the solution is quite
simple (recycling code, could be better):

```javascript
(function(Object, ArrayPrototype){
  if (typeof ArrayObject !== 'undefined') return;
  // the only difference
  (ArrayObject = function(){}).prototype = [];
  for(var
    modify = [
      'concat',
      'copyWithin',
      'filter',
      'map',
      'slice',
      'splice'
    ],
    keys = Object.getOwnPropertyNames(ArrayPrototype),
    create = function(method) {
      return function() {
        return Object.setPrototypeOf(
          method.apply(this, arguments),
          Object.getPrototypeOf(this)
        );
      };
    },
    i = keys.length,
    current, key;
    i--;
  ) {
    key = keys[i];
    current = Object.getOwnPropertyDescriptor(ArrayPrototype, key);
    if (~modify.indexOf(key)) {
      current.value = create(current.value);
    }
    Object.defineProperty(ArrayObject.prototype, keys[i], current);
  }
}(Object, Array.prototype));
```

and no old code will ever be affected so that in ES6 you can

```javascript

class Whatever extends ArrayObject {
  // that's it
}

```

and if you chose to extend `Array` the slice will produce an `Array`

I think this is a better alternative to potentially broken code ... but I
might be wrong.

Let's wait for this change to break out there, maybe it will not.

Cheers
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20130910/13740297/attachment-0001.html>


More information about the es-discuss mailing list