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


Groups > comp.lang.javascript > #29118

Re: array-like objects

Path csiph.com!news.mixmin.net!weretis.net!feeder1.news.weretis.net!news.solani.org!.POSTED!not-for-mail
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Newsgroups comp.lang.javascript
Subject Re: array-like objects
Date Sun, 03 Jan 2016 16:54:23 +0100
Organization PointedEars Software (PES)
Lines 84
Message-ID <1810599.sqNpoCiKvG@PointedEars.de> (permalink)
References <array-like-20160101185345@ram.dialup.fu-berlin.de> <n68q8c$ebf$1@speranza.aioe.org> <039150a8-45c4-4708-8b73-484c81df2ca2@googlegroups.com>
Reply-To Thomas 'PointedEars' Lahn <usenet@PointedEars.de>
Mime-Version 1.0
Content-Type text/plain; charset="UTF-8"
Content-Transfer-Encoding 8Bit
X-Trace solani.org 1451836463 23475 eJwNx8kNADEIBLCWgOEsB212+i8h+VhyIDW/8oz0YFD3yEJ8xmo12vpsY0abnCbwrzzA9y2bCwq4ELY= (3 Jan 2016 15:54:23 GMT)
X-Complaints-To abuse@news.solani.org
NNTP-Posting-Date Sun, 3 Jan 2016 15:54:23 +0000 (UTC)
User-Agent KNode/4.14.2
X-User-ID eJwFwYEBwCAIA7CXcNKC52iB/09Ygs1FhRN0DOZ6r+akvC6kKsljIWmdrSjmO2V70uOkDqrJMLM3X8Hv+gF7LxYH
Cancel-Lock sha1:ifgk1MGRhpxnlwrSk0Frbg91614=
X-NNTP-Posting-Host eJwFwYEBwDAIArCb7Cq6c4bI/ycsyQeBqYvETadH7KB2pMhIc+E63TSLK5Ralk/MOx8LP05+EvA=
Xref csiph.com comp.lang.javascript:29118

Show key headers only | View raw


Joao Rodrigues wrote:

> Joao Rodrigues escreveu:
>> 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 [].

What is “tricky to understand” about this?  The object does not have a (non-
undefined) length property, so the length of the resulting array must be 0.

>> 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

… after I corrected you the day before …

> that Array.from() is creating an array

IOW, you have confirmed that you were wrong.  An Array instance is not an 
“array-like object” in the sense that you used the term.

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

You don’t say…

  /* [object Array] */
  ({}).toString.call(Array.from({length: 3}))

As can be seen in the Firefox console output *in the OP*.

> 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]

Not surprising since you are mapping the “undefined”s of the property 
accesses …[0], …[1], and …[2] to their indexes 1, 2, and 3.  The above
can also be written

  Array.from(a, function (value, key) { return key; });

Incidentally, JSX has provided jsx.array.from() (for array-like objects) and 
jsx.array.fromObject() (for iterables) for several years before ECMAScript 
2015.

<http://PointedEars.de/wsvn/JSX?op=comp&compare[]=/trunk/object.js@392&compare[]=/trunk/object.js@393>
<http://PointedEars.de/wsvn/JSX?op=comp&compare[]=/trunk/@568&compare[]=/trunk/@569>

>> >    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.

You don’t say…
 
-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not cc me. / Bitte keine Kopien per E-Mail.

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