Syntax for variables within a namespace

  • Thread starter gimme_this_gimme_that
  • Start date
G

gimme_this_gimme_that

var BN = {
foo: function() {
alert("hello world");
},
z: function() {
alert("hello world");
},
}

I would like to add a member moo to BN whose definition is

var moo = {"a":"A","b","B"};

How do I do it?

Thanks
 
L

Lee

(e-mail address removed) said:
var BN = {
foo: function() {
alert("hello world");
},
z: function() {
alert("hello world");
},
}

I would like to add a member moo to BN whose definition is

var moo = {"a":"A","b","B"};

How do I do it?

The right hand side of your assignment to moo isn't valid Javascript syntax, so
there is no way to add it to BN. What are you really trying to do?


--
 
R

RobG

var BN = {
foo: function() {
alert("hello world");
},
z: function() {
alert("hello world");
},

Variable names starting with capital letters are usually reserved for
constructors. That last comma is a syntax error.

}

I would like to add a member moo to BN whose definition is

var moo = {"a":"A","b","B"};

Another stray comma, and property names don't need to be quoted in
object literals:

var moo = {a:"A", b:"B"};

How do I do it?

To add a property moo to the object BN:

BN.moo = {...};
 
G

gimme_this_gimme_that

Can you make the assignment withing the braces?

As in

BN = {
// create moo here
}
 
T

Thomas 'PointedEars' Lahn

RobG said:
Variable names starting with capital letters are usually reserved for
constructors.
ACK

That last comma is a syntax error.

Strictly adhering to the ECMA-262 Ed. 3 grammar, yes. Considering the
Conformance section of ECMA-262 (all editions so far), no.
[...] property names don't need to be quoted in object literals:

Not quite correct.
var moo = {a:"A", b:"B"};

It is only possible to omit the quote characters or apostrophes around
the property name here because `a' and `b' are identifiers.


PointedEars
 
D

Douglas Crockford

var BN = {
foo: function() {
alert("hello world");
},
z: function() {
alert("hello world");
},
}

I would like to add a member moo to BN whose definition is

var moo = {"a":"A","b","B"};

var BN = {
foo: function() {
alert("hello world");
},
z: function() {
alert("hello world");
},
moo: {"a": "A", "b": "B"}
}

or you could have augmented your original BN:

BN.moo = {"a": "A", "b": "B"};

http://javascript.crockford.com/
 
G

Gregor Kofler

Thomas 'PointedEars' Lahn meinte:
Strictly adhering to the ECMA-262 Ed. 3 grammar, yes. Considering the
Conformance section of ECMA-262 (all editions so far), no.

One should note, that IE (6) has it's problems with this extra comma. FF
and Opera (and suppose others) not.

Gregor
 
J

John G Harris

RobG wrote:


Strictly adhering to the ECMA-262 Ed. 3 grammar, yes. Considering the
Conformance section of ECMA-262 (all editions so far), no.
<snip>

The Conformance section is there to permit extra features. It's not
there to permit random typing errors. A certain amount of common sense
is assumed, of browser designers as well as of web designers.

John
 

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

Forum statistics

Threads
474,156
Messages
2,570,878
Members
47,408
Latest member
AlenaRay88

Latest Threads

Top