JavaScript Constructor Functions vs. Ruby Classes

Image from blog post

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);
         

Image from blog post

12/15/2015

So if you do not know what I just typed below you shouldn’t be worried, I am going to explain it. In short what I wrote is the template for a sloth. This template is called a class. Classes are used to organize methods and variables for objects together. You can think of methods being like actions the object can take and variables being its characteristics. A class is sort of like a blueprint for an Object (Which in programming can be pretty much anything).


                    class Sloth
                        attr_accessor :name, :species, :age

                        def initialize(name, species, age)
                            @name = name 
                            @species = species
                            @age = age
                        end

                        def yawn
                            puts “Arrraghoow #{@name} is tired”
                        end

                        def sleep
                            10.times{puts “Zzzzzzz”}
                        end

                        def eat(food)
                            puts “#{@name} is eating #{food}”
                        end

                        def climb
                            puts “A #{@age} year old #{@species} sloth named #{@name} is climbing a tree”
                        end

                        def print_info
                            puts “Name: #{@name}”
                            puts “Age: #{@age}”
                            puts “Species: #{@species}”
                        end
                    end

                    tim = Sloth.new(“Tim”, 3, “Three Toed”)

                    p tim.name

                    => “Tim”

                    tim.eat(“Mango”) 
                    => “Tim is eating Mango”

                    tim.print_info
                    => Name: Tim
                    Age: 3
                    Species: Three Toed
                 

So what do we have here. As you can see I am declaring a class named Sloth. So what that does is basically allows me to layout a blueprint for all the sloths my program will create. The attr_accessor is a bit more confusing but for now just view it as a way for us to access the variables (characteristics of the sloth) outside of our Sloth class. The first method called initialize is automatically performed whenever we create a new sloth, like we did for tim. The arguments we give the method are the characteristics we need to create our sloth, each sloth must have a name, age, and species! As you may have noticed we have prefixed some of our variables with the @ symbol. This is because this symbol indicates them as being Instance variables. This means they can travel between methods in the class and are available to every instance of the class, i.e every sloth we create.

All of the other methods besides initialize have to be called on. But as you may have noticed these methods are actions that a sloth would take, maybe with the exception of print_info. So this illustrates the point that methods in a class are sort of like actions the real world object would take. These are called instance methods because they are available to every instance of the Sloth class. So every sloth our program creates can yawn, sleep, eat, and climb, as well as print_info, but that is more of just for checking that our program is working right.

You now know the basics of writing classes in ruby. I suggest that you give it a try. Classes are used in Object Oriented Programming to imitate the way things work in the real world. So maybe make a class for Car, or Cat or even Liam's crappy old Subaru. Now go forth and code!