Sujet : Re: Indexation des tableaux en js
De : om+news (at) *nospam* miakinen.net (Olivier Miakinen)
Groupes : fr.comp.lang.javascriptDate : 10. Nov 2024, 12:22:48
Autres entêtes
Organisation : There's no cabale
Message-ID : <vgq529$2e02$1@cabale.usenet-fr.net>
References : 1 2 3 4 5 6 7 8 9 10 11 12 13
User-Agent : Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0 SeaMonkey/2.49.4
Le 10/11/2024 11:42, je mé répondais :
Bon, je me suis laissé avoir par l'affichage sur <https://runjs.app/play>.
Non, en réalité cet affichage était correct.
En réalité, tout index, qu'il soit fourni sous forme de nombre ou sous toute
autre forme, par exemple une chaine de caractères :
− est transformé en nombre s'il peut représenter un nombre entre 0 et 2^32-2 ;
− est transformé en chaine de caractères dans tous les autres cas.
La réalité d'après la norme ECMAScript :
<
https://tc39.es/ecma262/#sec-array-objects>
<cit.>
23.1 Array Objects
Arrays are exotic objects that give special treatment to a certain class of
property names. See 10.4.2 for a definition of this special treatment.
</cit.>
<
https://tc39.es/ecma262/#sec-array-exotic-objects>
<cit.>
10.4.2 Array Exotic Objects
An Array is an exotic object that gives special treatment to array index
property keys (see 6.1.7). A property whose property name is an array index is
also called an element. [...]
An object is an Array exotic object (or simply, an Array) if its
[[DefineOwnProperty]] internal method uses the following implementation, and its
other essential internal methods use the definitions found in 10.1. These
methods are installed in ArrayCreate.
</cit.>
<
https://tc39.es/ecma262/#sec-object-type>
<cit.>
6.1.7 The Object Type
[...]
The properties of an object are uniquely identified using property keys. A
property key is either a String or a Symbol. All Strings and Symbols, including
the empty String, are valid as property keys. A property name is a property key
that is a String.
An integer index is a property name n such that CanonicalNumericIndexString(n)
returns an integral Number in the inclusive interval from +0𝔽 to 𝔽(2**53 - 1).
An array index is an integer index n such that CanonicalNumericIndexString(n)
returns an integral Number in the inclusive interval from +0𝔽 to 𝔽(2**32 - 2).
</cit.>
Ce que je comprends de tout ça, c'est qu'un Array est un objet « exotique »,
et que comme tous ces types d'objets il a des propriétés dont le nom est soit
un symbole (par exemple « null » ou « true »), soit une chaîne de caractères.
J'en comprends aussi qu'il peut avoir au maximum 2^53 propriétés, mais que si
l'une de ces propriétés a comme nom une chaine de caractères représentant un
nombre entier entre 0 et 2^32-2, alors elle est considérée comme un index
numérique et bénéficie alors d'un traitement particulier, comme s'il s'agissait
vraiment d'un tableau indexé par des entiers. Et si par exemple on a un index
égal à 1 000 000 alors c'est comme si on avait vraiment réservé une taille
mémoire de 1 000 001 objets dont les premiers emplacements sont vides.
-- Olivier Miakinen