array syntax question

J

JJA

I'm looking at some code I do not understand:

var icons = new Array();
icons[""] = new GIcon();
icons[""].image = "somefilename.png";

I read this as an array of icons is being built.

An element of the array is an object itself but what is this syntax of
the consecutive double quotes inside the brackets ?

[ " " ]

Obviously, I am a rookie at Javascript. Thanks in advance.
 
V

VK

JJA said:
I'm looking at some code I do not understand:

var icons = new Array();
icons[""] = new GIcon();
icons[""].image = "somefilename.png";

I read this as an array of icons is being built.

An element of the array is an object itself but what is this syntax of
the consecutive double quotes inside the brackets ?

I have no idea. I mean I see what does happen but I have no idea why
such idiotic code would be needed. As a blind shoot: maybe the author
mistakenly thought that this way she was "hiding" or "protecting" the
property?

var icons = new Array();
This create icons array with 0 elements.

icons[""] = new GIcon();
This creates new instance of GIcon (which is a custom constructor
somewhere in the code). Then it creates new icons object property with
the name "" (empty string) and stores in it a reference to the newly
created GIcon instance. Property names used as string literal in quotes
are exempted from JavaScript naming rules, so you can create this way
properties named "" (empty string), "!@#$%^" etc.
All this has nothing to do with icons as an array instance: no new
elements are being added, icons.length remains zero.
 
R

Richard Cornford

JJA said:
I'm looking at some code I do not understand:

var icons = new Array();
icons[""] = new GIcon();
icons[""].image = "somefilename.png";

I read this as an array of icons is being built.

An element of the array is an object itself but what is
this syntax of the consecutive double quotes inside the
brackets ?

[ " " ]

Obviously, I am a rookie at Javascript. Thanks in advance.

It is often a source of confusion that where other languages have
special array accessing syntax javascript does not. It does, however,
have bracket notation property accessors, which resemble the special
array accessing syntax that is used in languages like Java.

Property accessors come in two flavours, the dot notation property
accessor like:-

document.body

- and the bracket notation property accessor, e.g:-

document['body']

Those two examples are functionally equivalent, they both access a -
body - property of an object referred to with the Identifier -
document -. Although they are equivalent in the sense of being specified
as using the same algorithm the differing syntaxes have implications on
what can be achieved with each type of property accessor.

First, with dot notation property assessors the component to the right
of a dot is required (by the syntax of the language) to be an
Identifier; it may not start with a decimal digit, be a (language or
future-reserved) keyword, boolean literal, null literal or be empty. So
while there are no specified restrictions on which character sequences
(including an empty sequence and sequences of decimal digits) may be
used in the property names of javascript objects only a sub-set of
possible property names may be accessed with a dot-notation property
accessor (this is a restriction imposed by the _syntax_, not by the
nature of the objects).

The practical result of this is that 'array index' property names cannot
be used in dot-notation property accessors because, beginning with
decimal digits, they can never qualify as Identifiers. This means that
all examples of accessing the 'array index' properties of an Array
object must use bracket notation property accessors, tending to
re-enforcing the false impression that bracket nation property accessors
are in some way array specific.

Second, bracket notation property accessors are syntactically defined as
having an Expression inside the square brackets, and resolving that
expression into a string that is used as the property name referred to
by the property accessor. String literals make for the most obvious
expressions in that context (and may contain, literally, any character
sequence), and numeric literals are often used in referencing 'array
index' properties of Array object, but the expression may be, for
example, an Identifier that refers to a local variable, where the value
of that variable used to determine the string that will be used to
reference a named property of an object. They may also be any more
complex expression, including function calls, where the return value of
the function call would provide the property name.

So while the syntax of a dot-notation property accessor requires that
the property names referred to be fixed at the point of writing the code
the property names bracket notation property assessors introduce the
runtime flexibility that can be expected in a language as dynamic as
javascript, as well as the freedom to use any character sequence as a
property name.

Although in your example the - icons - local variable is assigned an
Array there is nothing in the code that necessitates that the - icons -
object be an Array. The use of an empty string as a property name of
that object necessitates the use of bracket nation property accessor for
the assignment and any subsequent retrieval of the value of that
property, because an empty sequence of characters cannot qualify as an
Identifier, but - icons - could just as well (and maybe even, better)
have been assigned a reference to an Object object.

This use above may be a manifestation of confusing the object-general
bracket notation property accessor with some sort of array related
syntax, there are no shortage of examples where such misconceptions do
drive code authoring decisions. Thus you often see people assigning -
new Array() - where - new Object() - (or - {} -) would do the job just
as well.

The only thing that separates an Array object from an Object object
(apart from the array-related method that are defined on its prototype
chain and their having a - lenght - property of their own from the
outset) is that the Array object is given a special internal [[Put]]
method. All javascript objects are specified as using an internal
[[Put]] method for all assignment operations (and a corresponding
[[Get]] method for retrieval). The standard [[Put]] method is fairly
simple; it is passed a property names string and a value as an argument
and it does a quick internal check of the object using a method called
[[CanPut]] to see if the assignment is OK, and if so it assigns its
value argument to a property of the object with a name that corresponds
with the property name string argument (creating such a property if one
does not already exist).

All of the special behaviour associated with Array objects is accounted
for by their being given a special alternative [[Put]] method that has
an interest in the property name strings that are passed to it as
arguments:-

If the property name is "length" then the value is tested to verify that
it is (or will type-convert to) an unsigned 32 bit integer, and if it is
then a side effect of the assignment of a value to "length" is that any
'array index' propitious of the Array object that have 'array index'
values equal to or greater than this new "length" are deleted from the
object.

If the property name argument is an 'array index' (a value - P - for
which (String((P >>> 0)) === P) ) is true, and (P >>> 0) is less than (2
to the power of 32 minus 1)) then if (P >>> 0) is greater or equal to
than the value of object's - length - property then the - length -
property of the Array object is assigned a value equal to ((P >>> 0) +
1).

If the property name argument is anything else then the array's special
[[Put]] method does exactly the same as the standard object [[Put]]
method, assigning values to named properties of the object (and creating
properties where they did not previously exist).

This simple difference between the internal [[Put]] method of standard
object and the [[Put]] method of Array object explains all the
observable characteristics of an array object in javascript
(particularly their similarities with all other objects in javascript,
that is, they are really little different from all other objects in
javascript). It also explains why Array-ness cannot be inherited by
assigning an Array as the prototype of a constructor; the array's
special [[Put]] method is specific to the individual array instances and
so any object constructed with such a structure would have its own
[[Put]] method, and that [[Put]] method would be the normal Object
object version.

Richard.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top