Monday, October 9, 2017

Ruby vs. JavaScript




This week I begin really diving into JavaScript. I had studied a bit of the basics in Flatiron's Bootcamp prep course. but now that I understand how to work with Ruby, that feels distant. Over the weekend we had a bit of a refresher course in the homework, but there were several new concepts introduced as well. The entire time I found myself comparing what I now know about Ruby to what I was learning about JavaScript. Here are a few of the concepts that stood out to me.

Assignment and Comparison

In ruby = is used for assignment. It is used to set a variable. In Javascript this is the same. The real difference comes with the comparison operators. In Ruby we use the == to compare two values and return true if they evaluate to exactly the same. In Javascript the == is a non-strict way of doing this, which means if the type of the value on one side is not the same as on the other, JS will try to convert one so that they match. In JS it is more reliable to use === to compare two values since this version is strict, meaning it checks the equality of each value as well as the type, and if the type of the values doesn't match, it will return false. 

Objects

In Ruby, everything is an object. In JavaScript, an object is set to a variable and written as a hash. In both languages, a hash is a way of organizing key/value pairs. The Ruby objects are written slightly different as well (lots more semicolons!).The keys and values are both strings, Whereas in Ruby the keys would be symbols.



In JavaScript objects it seems we don't need to initialize them with a method the same way as in Ruby classes. They are initialized within a function with Object.create() or new Object() to create a new instance and set key/value pairs.  

Self and This

In Ruby we became very familiar with tracing the value of self when referring to multiple objects. It was used to refer to the current object that 'owns' the code currently running. That means within the class, it can refer to the class as a whole, or it can refer to a single instance of the class. 
The JavaScript equivalent to self is this. The keyword this,  when inside a function that creates an object, holds a reference to the most recent object initialized.


Prototype and Class
Every object in JavaScript inherits it's properties from a prototype. It seems at first glance that a prototype is equivalent to a super class in Ruby. However, they are rather different.  The JavaScript prototype is an object itself, and it is able to manipulate, describe or implement other objects. These abilities are the definition of a metaobject, which both Ruby classes and prototypes are. So whats the difference? Prototypes are a lot less private, the prototype is available to be used by many objects.  They are able to be chained together, each object inheriting methods and properties from the prototype before it. The Ruby object cannot store methods, but classes can. The Javascript method can store methods.


Hopefully this leaves you slightly less confused than I was this weekend!