The three main differences between JS and Ruby I’ve noticed thus far:
- Classes as functions?
Let’s go into these one by one.
The first difference that I noticed between Ruby and JS is how one goes about declaring variables and defining functions.
In Ruby, you can simply set any non-reserve word equal to any value, including null.
The process of declaring a value followed by setting it in JS is useful as most recently demonstrated by hoisting. “Hoisting is a mechanism where variables and function declarations are moved to the top of their scope before code execution.” This allows for variables to be available throughout the function regardless of their original global or local scope and makes variables available to us before they are declared.
Classes as Functions
Additionally, you can use the constructor function to create a class as well as functions within the class. These are usually denoted by a capital letter so they are easier to read in code.
Comparing it to Ruby, the constructor function takes on the task of creating a class and setting the properties and functions specific to instances of the class using “this” as opposed to “self”.
It is easy to add more complexity to the initial constructor function and also to access properties of an instance as well as utilize different functions and act upon these instances.
You can additionally extend classes and allow for them to inherit functions.
There is a lot of functionality that we’ll be learning about over the next few days.