Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #29108
| 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 | Sat, 02 Jan 2016 20:48:26 +0100 |
| Organization | PointedEars Software (PES) |
| Lines | 189 |
| Message-ID | <39241641.QMrUfM43MS@PointedEars.de> (permalink) |
| References | <array-like-20160101185345@ram.dialup.fu-berlin.de> <n68q8c$ebf$1@speranza.aioe.org> |
| Reply-To | Thomas 'PointedEars' Lahn <cljs@PointedEars.de> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset="UTF-8" |
| Content-Transfer-Encoding | 8Bit |
| X-Trace | solani.org 1451764110 19380 eJwFwQkBwDAIA0BLlCcwOVAW/xJ6F4aDm46AB4P+3SxmeSrLmN3c/9T0zozI1BigWCHV0vkAKJIRng== (2 Jan 2016 19:48:30 GMT) |
| X-Complaints-To | abuse@news.solani.org |
| NNTP-Posting-Date | Sat, 2 Jan 2016 19:48:30 +0000 (UTC) |
| User-Agent | KNode/4.14.2 |
| X-User-ID | eJwFwYEBwDAEBMCVCP9knCL2H6F3MCo7nKBjsQ32ZuROE3oPtTqqU9RMUPMqHsTc3v1If4kOfLQ+gqk5P1OUFWU= |
| Cancel-Lock | sha1:xCAC/Gxt2iUgmuW8jV63Mz3k7I8= |
| X-NNTP-Posting-Host | eJwFwQEBACAIA7BKHPmBOir2j+DGJehkiAo+PuxqEXcvdyQ4k9XZZeqkDCdq36AH3Ge1ffYYD2A= |
| Xref | csiph.com comp.lang.javascript:29108 |
Show key headers only | View raw
Joao Rodrigues wrote:
> 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,
No, as far as we know, he has _not_ declared a variable. Because that would
require “var a” or “var …, a” or “var …, a, …”. You have been told this
before. Most recently, only three days ago.
> containing just one key/value pair: length / 3. So, a is an object
> inheriting the Object prototype.
No, the value of “a” (likely “a” is a user-defined property of the global
object then) is a reference to an object inheriting from the object
initially referred to by the value of (the property) “Object.prototype”.
> Seemingly, the same could have been achieved with:
>
> var a = {};
> a.length = 3;
Not only seemingly.
> However, to see the real length of the object, you need to write:
>
> Object.keys(a).length // logs 1
Nonsense. You are missing the point.
>> 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.
“a” is not an object, it is an identifier. It might also be the name of a
property.
> That's why you are getting undefined whenever you try to access non
> existent properties of the object.
This has nothing to do with the fact that (the value of) “a” does not refer
to an Array instance. The question was explicitly about array-*like*
objects.
> Try a['whatever'] for instance.
Or “a.whatever”, since “whatever” is not (going to be) a reserved word.
>> 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".
Nonsense. Array.from() is explicitly designed to make use of references to
such objects, to convert them them into real Array instances.
> When you write Array.from(a), I think JavaScript is creating an
> array-like object with the length of 3 under the hood,
No, “JavaScript” does not do anything of the sort. Instead, a conforming
implementation of ECMAScript 2015, such as Google V8 JavaScript, creates an
*Array* instance with the length 3. Because *that* is the purpose of
*Array*.from().
> so that the code produces:
>
> [ undefined, undefined, undefined ]
That would not be an array-*like* object, now would it? And it is not; it
is an Array instance; the value of the internal “[[Class]]” property is
*obvious* from the posted console output…
> But I need to confirm that behaviour in the ECMA2015 Specification to be
> sure.
Why do you *still* not check your assumptions *before* you post? Nobody
needs your many misconceptions, and to waste their free time correcting
them.
<http://www.ecma-international.org/ecma-262/6.0/#sec-array.from>
>> 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().
Nonsense.
> See:
> <http://www.2ality.com/2013/05/quirk-array-like-objects.html>
You should stop believing blindly everything that you read or hear.
Barring a *standard* definition, an array-like object is one that behaves
like an array at least on property read access. Nothing more, nothing less.
For an object in an ECMAScript implementation that means that it has
indexes – properties whose names are unsigned integers – and that it has a
“length” property whose value indicates the greatest next usable index.
However, fortunately, the term “array-like” is used in ECMAScript 2015
several times, from which one can derive the definition I gave above:
| 6.2.1 The List and Record Specification Type
|
| […]
| For notational convenience an array-like syntax can be used to access List
| elements. For example, arguments[2] is shorthand for saying the 3rd
| element of the List arguments.
| 6.2.6 Data Blocks
|
| […]
| For notational convenience within this specification, an array-like syntax
| can be used to access the individual bytes of a Data Block value. This
| notation presents a Data Block value as a 0-origined integer indexed
| sequence of bytes. For example, if db is a 5 byte Data Block value then $
| db[2] can be used to access its 3rd byte.
| 7.1.15 ToLength ( argument )
|
| The abstract operation ToLength converts argument to an integer suitable
| for use as the length of an array-like object. […]
| 7.3.17 CreateListFromArrayLike (obj [, elementTypes] )
|
| The abstract operation CreateListFromArrayLike is used to create a List
| value whose elements are provided by the indexed properties of an array-
| like object, obj. […]
| 22.1.2.1 Array.from ( items [ , mapfn [ , thisArg ] ] )
|
| […]
| 7. Assert: items is not an Iterable so assume it is an array-like object.
| […]
| 22.2 TypedArray Objects
|
| TypedArray objects present an array-like view of an underlying binary data
| buffer (24.1). […]
| 22.2.2.1.1 Runtime Semantics: TypedArrayFrom( constructor, items, mapfn,
| thisArg )
|
| […]
| 9. Assert: items is not an Iterable so assume it is an array-like object.
| […]
| 23.1.1.1 Map ( [ iterable ] )
|
| NOTE
| If the parameter iterable is present, it is expected to be an object that
| implements an @@iterator method that returns an iterator object that
| produces a two element array-like object whose first element is a value
| that will be used as a Map key and whose second element is the value to
| associate with that key.
| 23.3.1.1 WeakMap ( [ iterable ] )
|
| […]
| If the parameter iterable is present, it is expected to be an object that
| implements an @@iterator method that returns an iterator object that
| produces a two element array-like object whose first element is a value
| that will be used as a WeakMap key and whose second element is the value
| to associate with that key.
--
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 | Next — Previous in thread | Next in thread | Find similar | Unroll 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