Mike's Blog

javaScript vs Ruby

Getting to the bottom of Ruby Classes and Javascript Constructors

September 29th 2015


The differences between Javascript Constructors/Prototypes and Ruby Classes

In Ruby a Class is a formal object within the ruby construct. it is a special kind of object with specific properties and methods. It is primarily responsible for creating new instances and defining the behavior of (methods within) its instances.

Instance behavior in Ruby is defined with a specific syntax. Changes to the class are allowed dynamically and are done by invoking methods on the class itself.

Javascript on the other hand, splits the responsibility for instances into constructor and prototype elements. A constructor in Javascript can be any function and constructors are the elements that are responsible for creating new instances (Class' are in the Ruby language). A prototype in JS can be any object and are responsible for defining what the instance will do. A big difference from Ruby is that prototypes don't have special methods or properties.

Instance behavior in Javascript relies on modifying the prototype directly by adding functions to it as properties. Another big difference is that there are no special syntax for defining or modifying a class.