By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,747 Members | 1,380 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,747 IT Pros & Developers. It's quick & easy.

Complex Variable

P: n/a
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

Oct 9 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Oct 9, 12:32 pm, Steve <stephen.jo...@googlemail.comwrote:
Could someone enlighten me regarding this method of
assigning values to a variable
http://developer.mozilla.org/en/docs...bject_Literals
ie what is the meaning of the ":"
Assignment of a value to a property
and why is no comma
required after the last line etc.
Its forbidden, IIRC, not simply 'not required'. Its a separator not a
terminator (so it goes between property/value pairs, not after them).

--
David Dorward
http://dorward.me.uk/
http://blog.dorward.me.uk/

Oct 9 '07 #2

P: n/a
That helps, many thanks.
On Oct 9, 4:24 pm, David Dorward <dorw...@gmail.comwrote:
On Oct 9, 12:32 pm, Steve <stephen.jo...@googlemail.comwrote:
Could someone enlighten me regarding this method of
assigning values to a variable

http://developer.mozilla.org/en/docs...5_Guide:Litera...
ie what is the meaning of the ":"

Assignment of a value to a property
and why is no comma
required after the last line etc.

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

--
David Dorwardhttp://dorward.me.uk/http://blog.dorward.me.uk/

Oct 9 '07 #3

P: n/a
David Dorward wrote:
On Oct 9, 12:32 pm, Steve <stephen.jo...@googlemail.comwrote:
>[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>
Oct 9 '07 #4

P: n/a
Thomas 'PointedEars' Lahn wrote:
In JavaScript 1.5 (in contrast to JScript),

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

is semantically equal to
var foo = {bar: "baz"};
Oct 9 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.