JavaScript Objects vs. Ruby Hashes
One of these things is...well, quite a bit like the other, actually
Nov 12, 2015
What's an object in JavaScript, and how does it work?
An object, in JavaScript, is an unordered list of data stored as a series of name-value pairs. The name can technically be either a string or a number, but it is generally best to avoid using numbers as a name. Values can be...well, strings, numbers, or other objects. Look at a sample:
var video_game = {
title: "Crusader Kings II",
genre: "Grand Strategy",
publisher: "Paradox Interactive"
} This is the object literal method for creating objects - probably the most common way that you will create them.
Hey, wait a minute...that looks an awful lot like...
Yup. That, my friends, looks pretty much just like the implicit form for creating a symbol-based Hash in Ruby. Really, the only difference is that Ruby doesn't need the "var" declaration; other than that, it looks precisely the same.
Are there any differences, then?
Of course there are. Coding wouldn't be fun without the wondrous variety of language, now would it? Some of the differences are:
- JS object properties can be accessed with a dot operator
or, like Ruby, with a key reference:console.log(video_game.title) => "Crusader Kings II"
The second method is much more useful when iterating over the properties of an object.console.log(video_game[title]) => "Crusader Kings II" - Object Constructors. These look a bit like Ruby class definitions - in other words, very different from the way Ruby hashes operate. So, say I knew I was going to make the above object for a bunch of video games; I might create a constructor that looked like this:
Note the "this" keyword - it basically serves the same function as the "@" sign in a Ruby class - defining the equivalent to an instance variable. Notably, however, the object constructor is incomplete when compared to a Ruby class definition, as it doesn't include methods for working with the data. It can include them, but they are more properly included in the object prototype...however, we're now getting a bit far afield from our stated comparison of Objects to Hashes.function VideoGame(title, genre, publisher) { this.title = title; this.genre = genre; this.publisher = publisher; } var video_game = new VideoGame("Crusader Kings II", "Grand Strategy", "Paradox Interactive"); - Object functions - the value in a name/value pair in a JS object can be a function, which can then be retrieved like any other value. So, here we'll add an example function to our video game object:
Again, this is something that, if it belongs to an object that will be used heavily, should be included in the prototype. Which is still outside the scope of this post.video_game.writeInfo = function() { console.log(this.name + " is a " + this.genre + " game published by " + this.publisher); } video_game.writeInfo; =>"Crusader Kings II is a Grand Strategy game published by Paradox Interactive"
So, in summary...
Objects in JavaScript are quite a lot like hashes in Ruby, and can be used as such, if you'd like. However, they're really much more flexible than that, with the ability to be expanded into something that much more closely resembles a Ruby class.