Complex Variable

S

Steve

I was advised by someone in the Google Maps API group to use this
method of assigning values to a variable:

var test = {
colour:"red",
sound:"music",
smell:"perfume"
};
alert(test.sound);

This works really well but I cannot find any information regarding how
this works.

Could someone enlighten me regarding this method of assigning values
to a variable, ie what is the meaning of the ":" and why is no comma
required after the last line etc.

Cheers
 
D

David Dorward

S

Steve

That helps, many thanks.


Assignment of a value to a property


Its forbidden, IIRC, not simply 'not required'. Its a separator not a
terminator (so it goes between property/value pairs, not after them).
 
T

Thomas 'PointedEars' Lahn

David said:
[Object initializer]
and why is no comma required after the last line etc.

Its forbidden, IIRC, not simply 'not required'.

It's not forbidden. The specification does not define the mechanism
explicitly, but it includes the provision for conforming implementations
to deviate from the specified program syntax (section "2 Conformance").

It is unwise to write the comma there because ECMAScript implementations
(particularly JavaScript and JScript) differ here; that is not an issue when
the used implementation is known (say in a Firefox extension).
Its a separator not a terminator
Correct.

(so it goes between property/value pairs, not after them).

In JavaScript 1.5 (in contrast to JScript),

var foo = {bar: "baz",};

is semantically equal to

var bla = {foo: "baz"};

It will show a warning in Geckos, though:

| Warning: trailing comma is not legal in ECMA-262 object initializers
| Source file: javascript: var foo = {bar: "baz",};
| Line: 1, Column: 23
| Source code:
| var foo = {bar: "baz",};


PointedEars
"Use any version of Microsoft Frontpage to create your site. (This won't
prevent people from viewing your source, but no one will want to steal it.)"
-- from <http://www.vortex-webdesign.com/help/hidesource.htm>
 

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
474,154
Messages
2,570,870
Members
47,400
Latest member
FloridaFvt

Latest Threads

Top