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


Groups > comp.lang.javascript > #29105

Re: array-like objects

Path csiph.com!news.mixmin.net!aioe.org!.POSTED!not-for-mail
From Joao Rodrigues <groups_jr-1@yahoo.com.br>
Newsgroups comp.lang.javascript
Subject Re: array-like objects
Date Sat, 2 Jan 2016 13:27:09 -0200
Organization Aioe.org NNTP Server
Lines 73
Message-ID <n68q8c$ebf$1@speranza.aioe.org> (permalink)
References <array-like-20160101185345@ram.dialup.fu-berlin.de>
NNTP-Posting-Host 5oq0M7qGeWhU/PcHEWLGrQ.user.speranza.aioe.org
Mime-Version 1.0
Content-Type text/plain; charset=windows-1252; format=flowed
Content-Transfer-Encoding 8bit
X-Complaints-To abuse@aioe.org
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.5.0
X-Notice Filtered by postfilter v. 0.8.2
Xref csiph.com comp.lang.javascript:29105

Show key headers only | View raw


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 }

You have declared the "a" variable using the Object literal notation, 
containing just one key/value pair: length / 3. So, a is an object 
inheriting the Object prototype. Seemingly, the same could have been 
achieved with:

var a = {};
a.length = 3;

However, to see the real length of the object, you need to write:

Object.keys(a).length // logs 1

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

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

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.

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



-- 
Joao Rodrigues

Back to comp.lang.javascript | Previous | NextNext 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