Ruby and JavaScript Syntax

Monday, September 22, 2014

The following table illustrates some syntactical differences between Ruby and Javascript. It is important to note that while Ruby is a class-based language Javascript is considered a prototype-based language.

In ruby, we organize classes to call instance methods on objects of that class to manipulate data. In Javascript, we can simulate Object Oriented Programming using functions to mimic classes. Using the constructor function on a new object in Javascript allows that object to inherit the prototype methods associated with that function. Instead of class methods, we say Javascript has static methods that cannot be called on an object, but rather on the class, or function itself.

Topic Ruby JavaScript
Class Declaration class ClassName
end
function Person(name, gender){
};
Instance Methods def method_name
end
ClassName.prototype.method_name = function() {};
Class Method def self.method_name
end
ClassName.method_name = function() {};
Object Instatiation object_name = Class_name.new() var object_name = new Class_name();
self vs. this
(current object reference)
self.method_name this.method_name;
Variable Declaration variable_name = some_value var variable_name = some_value
Instance Variables @variable_name = variable_name this.variable_name = some_value;