This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
When it gets to the point where I'm inserting data into the array, it's telling me "Unable to get value of the property 'colors': object is null or undefined". I've tried adding this:
but that didn't help either. Anybody know how to accomplish this?
In lines 4-8 you create a variable named "car" and assign it three attributes. The first two attributes ("make" and "model") get assigned String values, and the third ("arr") gets assigned something which is undefined.
Then in lines 10-12 you create a variable named "arrays" which contains only one attribute, an empty array. By coincidence that variable has the same name as the thing which was undefined at line 7.
Subsequently you never use the "arrays" variable, but you do attempt to use the "arr" attribute of the "car" variable, which as we already know was given an undefined value.
Well, the idea is that I'm trying to organize my variables a little better. Currently I have a massive blob of variables at the top. What I’d like to do is group them, perhaps by type. So, for example, I was thinking of having an object named “global” that would have some generic properties (make, model, etc). Then another object with several arrays (color, tire size, etc) as its properties. Then having that object be a property of the global object. So that I’d be able to do something like:
global.colors = [“red”, “maroon”];
So in the example above, I’m trying to have a type named “arrays” that contains arrays. Then I ‘m trying to make a property in the “car” object, named “arr”, that is of the type “arrays”. Eventually the “arrays” object might contain several arrays that I can interact with by saying “car.arr.<arrayName>”
Hope this makes sense.
That all strikes me as just kinda weird. Firstly, just as with any other language, globals need to be minimized or avoided, so formalizing them in this way actually seems counter to any proper approach.
Closures help to keep variables out of the global scope. I suggest some reading up on them.