tags:

views:

50

answers:

3

Set an object, with some data.

var blah = {};
blah._a = {};
blah._a._something = '123';

Then wish to try and access, how would I go about doing this correctly?

var anItem = 'a';
console.log(blah._[anItem]);
console.log(blah._[anItem]._something);
+6  A: 

The bracket notation should look like this:

var anItem = 'a';
console.log(_glw['_'+anItem]);
console.log(_glw['_'+anItem]._something);

You can test it here (note that I replaced _glw with blah in the demo to match the original object).

Nick Craver
A: 

Not sure I understand the question, but here are some basics.

var foo = {};

// These two statements do the same thing
foo.bar = 'a';
foo['bar'] = 'a';

// So if you need to dynamically access a property
var property = 'bar';
console.log(foo[property]);
Znarkus
A: 
var obj = {};
obj.anotherObj = {};
obj.anotherObj._property = 1;
var item = '_property';
// the following lines produces the same output
console.log(obj.anotherObj[item]);
console.log(obj.anotherObj['_property']);
console.log(obj.anotherObj._property);
Lekensteyn