A simple class
Here is a class representing the polygon:
class RegPolygon
this.sides = 3 // Default number of sides
this.length = 1 // and default length
function perimeter( )
return this.sides * this.length
endclass
The top of the CLASS definition, up to the first FUNCTION, is called the class constructor, which is executed when an instance of the class is created. In the constructor, the reference this refers to the object being created. The sides and length properties are added, just as they were before.
The function in the class definition is considered a method, and the object automatically has a property with the same name as the method that points to the method. The code is the same, but now instead of a codeblock, the method is a function in the class. Methods have the advantage of being easier to maintain and subclass.