Day 5: Eloquent JavaScript Chapter 4 and looping through JavaScript Objects

So, I’ve decided to start a blog for myself to track my progress in learning JavaScript. It’s been 5 days since I commenced my ‘become a devastatingly effective JavaScript ninja’ pursuit.

My primary learning resources to date (inspired by HackReactor’s recommendation) have been:

Chapter 4 Of Eloquent JavaScript: Data Structures, Objects & Arrays (And My Issue With The Exercises)

I finished Chapter 4 of Eloquent Javascript, but not quite successfully. I think part of the problem was that the author didn’t really make explicit all the assumptions that we were to be coding against. This resulted in me attempting to create functions to cover different variable names, when the author’s answers assumed that the variable names would usually remain static.

Another issue for me (possibly something I need to double check though) was the author using techniques that he hadn’t really exposed prior to the exercises. For example, in the final exercise ‘Deep Comparison’, the author makes use of the ‘for (var someProperty in someObject)’ looping expression, which is supposed to loop through each property in an object. That’s fine, but the author’s answer used this looping expression to loop through TWO separate objects, in a way that he hadn’t described in his lessons.

I know about this:

for (var property in object) {
// whatever you want to do to the object

so if you wanted to change each property’s value in an object, you could do this:

for (var someProperty in someObject) {
someObject[someProperty] = “” // this would replace each object’s property value with an empty string

but apparently, you can also do this:

for (var someProperty in someObject) {
someObject[someProperty] = “” // this would replace each object’s property values with an empty string
someOtherObject[someProperty] = “” // this would replace a SECOND object’s property values with an empty string

it only works of course if you have two existing objects previously declared/initialised (still grasping the terminology…) and the name of the properties in each object have to exactly match each other, or you’ll end up with undefined at some point. So if you had:

var someObject = {property1: “words”, property2: “more words”};
var someOtherObject = {property1: “big words”, property2: “more big words};

then using the looping expression for objects can be performed on both objects during the same loop, because someOtherObject has the same names for its properties (even if the values held in the properties are different). But, if you had this instead:

var someObject = {property1: “words”, property2: “more words”};
var someOtherObject = {propertyA: “big words”, propertyB: “more big words};

the looping expression would work for someObject (because it’s using someObject’s property names which are of course, valid in someObject), but not for someOtherObject (because during the loop at some point, the program will attempt to execute the below:

someOtherObject[property1] = “”

and of course, it can’t because someOtherObject doesn’t have a property by that name, rather it has properties by the name of ‘propertyA’ and ‘propertyB’.

What Else I’ve Been Doing

That’s the end of my rant today. I’ve also started reading through JavaScript Cookbook 2nd Edition today, which I’m finding to be an excellent resource. I’ve currently covered things like little useful string methods (string.split, string.trim, string.indexOf) and a way to loop through arrays (.forEach, which is destructive, but I haven’t really looked into .map yet). My short read has also exposed me to some other ideas such as method chaining, which I’ll be taking a look at a little bit more, though Googling this tends to turn up depressingly large amount of jQuery content (I’m not there yet!).

*Note to self: remember comparison operators in JavaScript include == and the stricter ===, but definitely not = which is strictly for assigning values. (and just when I thought I was over basic syntax…)

JavaScript ninja in training, signing out;

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s