After spending the last few weeks learning the fundamentals of Ruby, we started learning some basics of JavaScript. Having a background in Ruby definitely made the transition easier for me. I was able to clearly recognize objects in Javascript and how they were being manipulated. With that said, JavaScript features some pretty distinct aspects from Ruby. In this blog, I'll explore how object data is stored in JavaScript vs. Ruby.
If we refer back to some of my older posts on classes, we know that objects are initialized under a specific class with instance variables that define states of the object. While it's possible to access these states through attr_readers, accessing the data in larger stacks of code can sometimes be tricky. In JavaScript, the states of objects are refered to as properties and much easier to access. Let's look at an example of object and property syntax in JavaScript:
var person = {
name: bill,
age: 28,
eyeColor: blue,
};
In the above example, we have an object 'person', with the properties name, age, and eyeColor. If we want to access the values of these properties, we simply write:
person.name;
person.age;
person.eyeColor;
In Ruby, these properties would not be readily accessible. As mentioned above, we would have to set them to attr_readers to return a value. In JavaScript, it's also relatively easy to write new values to a property that already exists. Example:
person.eyeColor = green;
To do this in Ruby, we'd have to set the properties (instance variables/states) to attr_writers or attr_accessors.
So far, we've mainly focused on how it's easier to access property values of objects in JavaScript. It's worth mentioning, however, that the data structure of an object in JavaScript is very similar to a specific kind of object in Ruby—a hash. In Ruby, a hash has key-value pairs, with syntax that closely mirrors JavaScript:
person = {name: "bill", age: 28, eyeColor: "blue"}
In the above example, person is a Ruby hash, while name:, age:, and eyeColor: are the keys, with corresponding values of "bill", 28, and "blue", respectively. It's relatively easy to access the value in a hash based on its key:
person[:name] => "bill"
The syntax for accessing the value is different than JavaScript, but the pairing is relatively similar.