0

Sorry for the newb question. But I have been trying for hours.

var dbruns = {}; // namespace

dbruns.dbstuff = {
    var nameslistresult = [];

This gets an error, unexpected identifier. When I put it at the dbruns level

var dbruns = {}; // namespace
var nameslistresult = [];
dbruns.dbstuff = {

It gets past there, but then

console.log(typeof dbruns.nameslistresult);
console.log(typeof dbruns.dbstuff.nameslistresult);

both return undefined.

I really have no idea what is going on here, and any help would be appreciated.

3
  • 2
    dbruns.dbstuff = {nameslistresult:[] }; or dbruns.dbstuff = {};dbruns.dbstuff.nameslistresult = []; Have you already tried to read documentation or tutorials before "trying for hours"? Commented Aug 31, 2014 at 14:32
  • Thank you so much! Indeed I did, I didn't see anything about how to define properties. I presumed they were public variables. Since they kind of are. Commented Aug 31, 2014 at 17:37
  • Just to save a bit of face, the thing that drove me to try ever more bizarre attempts at making the code work was because the underlying API was broken. Commented Sep 1, 2014 at 15:05

2 Answers 2

3

You appear to be confusing variables and properties.

This is how you declare a variable (and assign it an empty object as a value using the object literal syntax):

var dbruns = {};

This is how you assign a value to a property of an object:

dbruns.dbstuff = { …

This is how you construct an object literal with predefined properties:

… = {
    nameslistresul: [],
    foo: bar
};
Sign up to request clarification or add additional context in comments.

Comments

1

Unfortunately javascript does not have name-spaced blocks, but rather functions act as enclosures.

The first error you are getting is because you are not defining a namespace, but defining a javascript object, and then puting unexpected expressions in there instead of property definitions...

var dbruns = { key: "value", another_key: "another value" } // valid syntax for object definition

If you want nameresults to be a property of dbruns you can do...

var dbruns = {}
dbruns.nameresults = []
dbruns.stuff = {}

Javascript uses prototypal inheritance, and everything in javascript is an object.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.