JavaScript Constructor Functions vs. Ruby Classes
12/21/2015

Ruby classes are templates for objects in ruby programming. JavaScript constructors are Prototypes for objects in JavaScript Programming. So why the difference and how do these techniques relate and differ? Lets dive right into that!
JavaScript is a classless Object Orientated Programming (OOP) language. What this means is that JavaScript accomplished the feats of classes in languages like ruby by using a special type of function called a constructor. A constructor function basically lays out the template for all Objects that inherit from the constructor object as well as all instances of the constructor object.
Ruby is an OOP language as well. In ruby however you can and must use classes to lay out the format for objects. In Ruby each class has an initialize method that is called when an instance of the class is created. Ruby classes can also have instance methods, which are the same thing as functions in JavaScript, Instance methods basically allow any instance of that class to have access to those methods.
Ruby classes and JavaScript constructor functions are similar in that they both are templates for all new instances of the themselves. In ruby you can add methods (i.e functions) right into the class however in JavaScript it is common practice to add functions (i.e methods) with a prototype method. This allows for all instances of the constructor function to have the added functions. This is one major difference. Another is that the syntax for constructor functions and classes is totally different. Take a look at last week’s post about the Ruby sloth class then see the partially completed JavaScript sloth constructor function. In reality both JavaScript constructor objects and Ruby Classes get the same job done, but I would say I find the idea of classes easier to implement and understand than constructor functions. Now go forth and code!
var Sloth = function(name, species, age){
this.name = name;
this.species = species;
this.age = age;
};
var timmy = new Sloth("Timothy Purple Haze Stweart", "Three Toed", 4);