473,387 Members | 1,669 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

array syntax question

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.

Dec 15 '06 #1
2 2464
VK
JJA wrote:
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.

Dec 15 '06 #2
JJA wrote:
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.
Dec 16 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Geoff Cox | last post by:
Hello, Can I separately declare and initialize a string array? How and where would I do it in the code below? It was created using Visual C++ 2005 Express Beta 2 ... In C# I would have ...
4
by: bienwell | last post by:
Hi all, Data displayed on the datalist control is bound by the column name of the dataset like this : <%# DataBinder.Eval(Container.DataItem, "title")%> Could I use an element of the array...
4
by: Alan Foxmore | last post by:
Hi everyone, I'm new to C# and I was hoping I could get some clarification on the syntax for jagged and multidimensional arrays. Here is my question: The following syntax is correct for...
7
by: Sam Kong | last post by:
Hello! My question would not be very practical but just out of curiosity. In JavaScript, Array is a subclass of Object. Well maybe not exactly... but sort of... For normal objects, you can...
272
by: Peter Olcott | last post by:
http://groups.google.com/group/comp.lang.c++/msg/a9092f0f6c9bf13a I think that the operator() member function does not work correctly, does anyone else know how to make a template for making two...
24
by: VijaKhara | last post by:
hi all, i am trying to create a dynamic 2D array with size N x 3 (N will be put in as a parameter) using the following code: int **xyz; int i,N; N=30000; xyz=malloc(3*sizeof(int*));
7
by: roguefeebo | last post by:
I'm very new to programming so be gentle, :( Essentially what I would like to do is to have a single function to be able to create a dynamic array of pointers to a struct so that I can have a...
18
by: mdh | last post by:
>From p112 ( K&R). Given an array declared as static char arr= { { 0,1,........},{0,1,.....}}; let arr be passed as an argument to f. f( int (*arr) ) {....} It is noted that the...
2
by: berrylthird | last post by:
This question was inspired by scripting languages such as JavaScript. In JavaScript, I can access members of a class using array syntax as in the following example: var myInstance:myClass = new...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.