In JavaScript methods (aka member functions) are functions that belong to objects. Member functions are designed to operate directly on the properties of the object to which they belong.
function Circle (radius) {
this.radius = radius;
this.area = function () {
return Math.PI * this.radius * this.radius;
};
// define a perimeter method here
this.perimeter = function () {
return 2*Math.PI*this.radius;
};
}
In the code above the constructor “Circle” (similar to a class in Ruby) has two methods (area, and perimeter).
Another tidbit is that properties in JavaScript are the equivalent to Ruby Hash Keys.
Stay connected