1

Not sure what to search for here, so apologies if I'm repeating another question.

I'm wondering if there are any issues that I'm not aware of with using the following syntax in JavaScript:

var a = {};

var b = a.niceCoat = {};

Seems handy, but I just want to make sure...

3
  • 1
    There doesn't seem to be anything wrong with it. Which kind of "issues" are you thinking about? Semantic pitfalls, readability, maintainability, cross-browser compatibility? Commented Aug 30, 2011 at 17:35
  • Hej Henning, just was really thinking that I'd check for all the above with more experienced coders! Readability seems ok to me, maintainability too I guess, but I am inexperienced. Cross browser stuff is a gap in my knowledge for sure. Commented Aug 30, 2011 at 17:51
  • Issues as in what? Commented Apr 5 at 8:34

5 Answers 5

10

That is perfectly fine, because a was declared previously. The expressions will be evaluated as

var a = {};
var b = (a.niceCoat = {});

I.e. it first assigns a new empty object to a.niceCoat and the result (the result of an assignment is the assigned value) to b.

But be aware of something like

var a = b = 'c';

which, again, is evaluated as

var a = (b = 'c');

Only a will be in local scope, b would be global. If you want b to be local too, you have to declare it beforehand: var b;. Something like var a = var b = .... does not work (not valid syntax).


Slightly off topic:

This method is indeed handy. Imaging you have an object of objects, something like:

var map = {
    foo: {},
    bar: {}
};

and you want to get the object for a certain key or create a new one if the key does not exists. Normally one would probably do:

var obj = map[key];
if(!obj) { // works because if the key is set, it is an object 
    obj = {}; //                                        which evals to true
    map[key] = obj;
}
// now work with obj

With the above method, this can be shortened to

var obj = map[key];
if(!obj) {
    map[key] = obj = {};
}

And we can make it even shorter with the logical OR operator (||):

var obj = map[key] || (map[key] = {});

(though it might be less readable).

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks @Felix. This is really great advice and just the sort of thing I was looking for!
@Richard: You're welcome :) I added another use case for this method (in case you are interested, though I fear that it might be too off topic).
@Felix, whatever about off-topic, the willingness to share in the web community makes me feel all warm inside. Plus this will come in very handy with something I'm working on right now! Many thanks.
3

You can do that. a.niceCoat = {} will be evaluated first, which assigns the object to the property and also has the object as result, which you then can assign to the variable.

You should however be aware that b and a.niceCoat are now referencing the same object, so if you put anything in the object it will show up for both the variable and the property:

var a = {};
var b = a.niceCoat = {};

b.x = 42;
alert(a.niceCoat.x); // shows 42

2 Comments

+1 for mentioning that both reference the same object. I thought the OP was aware if this, but you are right in pointing it out.
@Guffa - cheers for this. Again, just the sort of stuff I wanted to know. Many thanks.
0

There no issue with that. It's the same as:

var b = (a.niceCoat = {});

Which is the same as:

a.niceCoat = {};
var b = a.niceCoat; // Now b and a.niceCoat are the same object

Just be careful with declaring entirely new variables with it like:

var i = j = 0;

Which is the same as:

j = 0;
var i = j;

Notice how j is not declared with the var keyword.

Comments

-1

this is how you create an empty object in javascript. nothing wrong with it.

Comments

-1

Yep, absolutely valid.

E.g.

var a = {};

var b = a.niceCoat = {};

a.foo = function() { alert('foo!'); };


a.foo(); // shows the alert

1 Comment

@Richard Sweeney, this notation is especially useful for creating namespaces in javascript. Check out these examples: stackoverflow.com/questions/881515/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.