Array.prototype.slice web-compat issue?

Andrea Giammarchi andrea.giammarchi at gmail.com
Tue Sep 10 16:38:16 PDT 2013


right now? the `[[Class]]` such as `'[object Array]'` ... and the magic is
this smarter slice.

I insist this would be way better and already possible today:

```javascript
(function(Object, ArrayPrototype){
  if (typeof ArrayObject !== 'undefined') return;
  ArrayObject = function(){};
  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, key, current);
  }
}(Object, Array.prototype));
```

where any class that extends `ArrayObject` will have the new behavior and
less problem with the past.

```javascript
var a = new ArrayObject;
a.push(1, 2, 3);
a.slice() instanceof ArrayObject; // true
```

br


On Tue, Sep 10, 2013 at 4:32 PM, Allen Wirfs-Brock <allen at wirfs-brock.com>wrote:

>
> On Sep 10, 2013, at 4:11 PM, Andrea Giammarchi wrote:
>
> > Rick I think I've often seen this which is not that naive accordingly
> with ES 5.1 down to ES 3 specs, IMO .. it worked with DOM, arguments, and
> everything with a `length` till now.
> >
> > ```javascript
> > Array.from||(Array.from=function(a){return
> Array.prototype.slice.call(a)});
> > ```
> >
> > Allen the `%TypedArray%` is a good example because this is false: `new
> Float32Array instanceof Array` ... that constructor has even the suffix
> with the `Array` word but is not an `Array` at all.
> >
> > `Array.isArray(new Float32Array)` is again false indeed.
> >
> > I also think this magic should somehow be something new, hoping nobody
> will think about creating polyfills for all Array methods because once
> again that will be very bad for the web/mobile/ARM world, IMO.
>
> Which magic are you talking about.  Array.isArray is defined for ES6 to
> test whether an object has the length invariant automatically enforced.
> That's really the only thing that makes an Array instance special (or in
> ES6-speak, exotic). Float32Arrays do not have that length invariant because
> their length is fixed.
>
> What do you think people are actually testing for when they do
> Array.isArray?
>
> Allen
>
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20130910/2306a003/attachment.html>


More information about the es-discuss mailing list