What does this mean?

L

listerofsmeg01

Newbie here again,

I was wondering if Javascript supported enumerations, so a quick search
in this group turned up the following code:

var eColors = new {red:0, yellow:1, green:2, blue:3};

This works just fine, but I was intrigued as to what is actually going
on here. Obviously an array called eColors is being created, but I
don't understand the ":" notation on the array elements. What exactly
does this do?

Many thanks
 
E

Evertjan.

wrote on 23 okt 2006 in comp.lang.javascript:
Newbie here again,

I was wondering if Javascript supported enumerations, so a quick search
in this group turned up the following code:

var eColors = new {red:0, yellow:1, green:2, blue:3};

This works just fine,

It does NOT:

"new {}" does not exist, "{}" does.

try and learn:

<script type='text/javascript'>

var eColors = {red:0, yellow:1, green:2, blue:3};

document.write(typeof eColors)
document.write('<br>')

document.write(eColors['red'])
document.write('<br>')

document.write(eColors['yellow'])
document.write('<br>')

document.write(eColors['blue'])
document.write('<br>')

</script>
 
R

RobG

Newbie here again,

I was wondering if Javascript supported enumerations, so a quick search
in this group turned up the following code:

var eColors = new {red:0, yellow:1, green:2, blue:3};

This works just fine, but I was intrigued as to what is actually going
on here. Obviously an array called eColors is being created,

Not obviously at all. The "new" keyword is out of place, the rest of
the right hand side is an object literal or initialiser:

var eColors = {red:0, yellow:1, green:2, blue:3};

<URL:
http://developer.mozilla.org/en/doc...reating_New_Objects:Using_Object_Initializers
but I
don't understand the ":" notation on the array elements. What exactly
does this do?

That is part of object literal notation. The statement, taken as a
whole, declares a new variable called 'eColors' and assigns it a
reference to the object created by the expression on the rhs.
 

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,774
Messages
2,569,598
Members
45,158
Latest member
Vinay_Kumar Nevatia
Top