Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #29115

Re: array-like objects

Newsgroups comp.lang.javascript
Date 2016-01-03 04:26 -0800
References <array-like-20160101185345@ram.dialup.fu-berlin.de> <n68q8c$ebf$1@speranza.aioe.org>
Message-ID <039150a8-45c4-4708-8b73-484c81df2ca2@googlegroups.com> (permalink)
Subject Re: array-like objects
From Joao Rodrigues <groups_jr-1@yahoo.com.br>

Show all headers | View raw


Joao Rodrigues escreveu:
> On 01/01/2016 04:11 PM, Stefan Ram wrote:
> >    I define an object as follows (one line of input, one line
> >    of output from the firefox console):
> >
> > a = { length: 3 }
> > Object { length: 3 }


> >
> >    Now I observe the following three evaluations:
> >
> > a[ 0 ]
> > undefined
> >
> > a[ 1 ]
> > undefined
> >
> > a[ 2 ]
> > undefined
> 
> As noted above, "a" is an object, not an array. That's why you are 
> getting undefined whenever you try to access non existent properties of 
> the object. Try a['whatever'] for instance.

Try a['length'] or a.length and you'll get 3, because there is such property in the object.


> >    . So, is »a« now an »array-like object«, because it has
> >    a lenght between 0 and 9007199254740991 and it has three
> >    entries for the numeric keys 0 <= key < lenght, which
> >    just so happen to be »undefined«?
> >
> > b = Array.from( a )
> > Array [ undefined, undefined, undefined ]
> 
> Array.from() is supposed to be used with array-like or iterable objects 
> such as Map and Set. So you cannot pass non iterable objects such as 
> your "a".

In reality, Array.from() also accepts non iterable objects such as { length: 3 }, but the result can be tricky to understand, as Array.from() was designed to work with array-like or iterable objects

E.g. Array.from({g:50, t:20}) produces [].

> 
> When you write Array.from(a), I think JavaScript is creating an 
> array-like object with the length of 3 under the hood, so that the code 
> produces:
> 
> [ undefined, undefined, undefined ]
> 
> But I need to confirm that behaviour in the ECMA2015 Specification to be 
> sure.

I have confirmed that Array.from() is creating an array, following the steps of the ES6 Specification, section 22.1.2.1:
<http://ecma-international.org/ecma-262/6.0/index.html#sec-array.from>

Let's recap the example:

  var a = { length: 3 }
  Array.from(a) // [undefined, undefined, undefined]

Now, let's comment on the relevant steps of the Array.from()

22.1.2.1 - Array.from ( items [ , mapfn [ , thisArg ] ] )
[...]
Step 10 - Let len be ToLength(Get(arrayLike, "length")).

The abstract operation Get is used to retrieve the value of a specific property (P) of an object (O), as specified in section 7.3.1. So Get(O,P) will be Get(a,"length"), and we know that a.length returns 3.

  let len = toLength(a.length); // 3

Step 13 - Let A be ArrayCreate(len). The abstract operation ArrayCreate is defined in section 9.4.2.2.

  let A = new Array(len);

Step 15 - Let k be 0.
Step 16 - Repeat, while k < len. And following steps a thru h, we have something like:

  while (k < len) {
    v = items[k]; // v = a[k], so v = undefined.
    if (mapFn) {
       //
    } else {
      A[k] = v; // A[k] = undefined.
    }
    k = k + 1;
  }

Step 17 - Let setStatus be Set(A, "length", len, true).

Step 19 - Return A.

So, after all these steps, Array.from(a) will return the array
[undefined, undefined, undefined].

But if you write:

  var a = { length: 3 }
  Array.from(a, (value, key) => key);

FF and Chrome console will print out [0, 1, 2], instead of [undefined, undefined, undefined]


> >    So, the only thing we require for an object to be »array-like«
> >    is a length property with an appropriate value?
> 
> No, array-like objects must have:
> - indexed access to elements and the property length that tells us how 
> many elements the object has.
> - do not have array methods such as push(), forEach() and indexOf().
> 
> See:
> <http://www.2ality.com/2013/05/quirk-array-like-objects.html>
> 
 
For short, "array-like" objects have a length property and indexed elements.

-- 
Joao Rodrigues

Back to comp.lang.javascript | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Re: array-like objects Joao Rodrigues <groups_jr-1@yahoo.com.br> - 2016-01-02 13:27 -0200
  Re: array-like objects Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-01-02 20:48 +0100
    Re: array-like objects John Harris <niam@jghnorth.org.uk.invalid> - 2016-01-03 15:50 +0000
      Re: array-like objects Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-01-03 16:59 +0100
        Re: array-like objects John Harris <niam@jghnorth.org.uk.invalid> - 2016-01-04 11:15 +0000
          Re: array-like objects Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-01-04 15:27 +0100
            Re: array-like objects Tim Streater <timstreater@greenbee.net> - 2016-01-04 15:57 +0000
            Re: array-like objects John Harris <niam@jghnorth.org.uk.invalid> - 2016-01-05 16:47 +0000
              Re: array-like objects Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-01-05 20:13 +0100
                Re: array-like objects John Harris <niam@jghnorth.org.uk.invalid> - 2016-01-06 16:24 +0000
                Re: array-like objects Aleksandro <aleksandro@gmx.com> - 2016-01-06 14:45 -0300
                Re: array-like objects Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-01-06 19:27 +0100
                Re: array-like objects John Harris <niam@jghnorth.org.uk.invalid> - 2016-01-07 11:00 +0000
                Re: array-like objects John Harris <niam@jghnorth.org.uk.invalid> - 2016-01-08 17:02 +0000
                Re: array-like objects John Harris <niam@jghnorth.org.uk.invalid> - 2016-01-09 20:07 +0000
                Re: array-like objects Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-01-10 09:03 +0100
                Re: array-like objects John Harris <niam@jghnorth.org.uk.invalid> - 2016-01-10 10:03 +0000
                Re: array-like objects Andrew Poulos <ap_prog@hotmail.com> - 2016-01-10 23:21 +1100
                Re: array-like objects John Harris <niam@jghnorth.org.uk.invalid> - 2016-01-10 15:59 +0000
                Re: array-like objects John Harris <niam@jghnorth.org.uk.invalid> - 2016-01-10 15:30 +0000
                Re: array-like objects John Harris <niam@jghnorth.org.uk.invalid> - 2016-01-11 11:03 +0000
                Re: array-like objects "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2016-01-11 10:36 -0800
                Re: array-like objects John Harris <niam@jghnorth.org.uk.invalid> - 2016-01-12 10:59 +0000
                Re: array-like objects "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-01-12 12:47 +0100
                Re: array-like objects John Harris <niam@jghnorth.org.uk.invalid> - 2016-02-06 10:50 +0000
                Re: array-like objects Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-02-09 05:01 +0100
                Re: array-like objects John Harris <niam@jghnorth.org.uk.invalid> - 2016-02-09 19:07 +0000
                Re: array-like objects John Harris <niam@jghnorth.org.uk.invalid> - 2016-02-09 19:23 +0000
  Re: array-like objects Joao Rodrigues <groups_jr-1@yahoo.com.br> - 2016-01-03 04:26 -0800
    Re: array-like objects Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-01-03 16:54 +0100
      Re: array-like objects Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-01-04 00:18 +0100

csiph-web