Skip to content
Related Articles

Related Articles

CoffeeScript Class

View Discussion
Improve Article
Save Article
  • Last Updated : 31 Jan, 2022

CoffeeScript is an object-oriented programming language. Classes make large code readable, and easy to maintainable. Objects are instances of a class, a real-world entity that can be a person, place, string, list, etc. Data members are the variables declared inside the class. In this article, we will understand how the class work in CoffeeScript.

Prerequisites: You can either run code samples on the online CoffeeScript compiler or you must have CoffeeScript installed on your system. Refer to this, https://www.geeksforgeeks.org/coffeescript-introduction/

To run CoffeeScript on your system, use the below command,

coffee fileName.coffee

Class: Class is a prototype to create objects. Classes bind data and functionality together. Instantiating the class creates a new type of object each time. Each class instance has its attributes and methods. Class is a user-defined data structure to promote code readability and easy maintenance.

Some points on CoffeeScript class

  • The “class” keyword is used to define a class.
  • Attributes are the variables or data members that belong to the class.
  • Methods are variable functions belonging to the class.
  • Attributes and methods are by default public.
  • Attributes and methods can be accessed using (.) operator on objects.

Class Definition Syntax:

class Name_Of_Class
    statement-1
    statement-2
    .
    .
    .
    statement-N

Example:  In this example, a p1 object is created which instantiates the class MyClass and print “I am the class”. Then, we invoked method fun on the p1 object, which executes fun() and print “I am the class method”. We will learn more about objects and methods later in this article. The output of the above example is shown below: 

Javascript




class MyClass
    console.log "I am the class"
  
    fun: () -> 
       console.log "I am the class method"
  
p1 = new MyClass #Here, we are creating an object
p1.fun() #Here, we are calling method of class on an object


Output:

I am the Class
I am the Class Method

Constructors: Constructors are defined to instantiate the object’s class. Constructors are like methods containing statements to be executed. The constructor will be executed when the class is instantiated. If the constructor requires any parameters, we need to give values to it at the time of object creation. In the below example, class Person contains a constructor which takes a name as a parameter. We are passing the value to the name parameter with the class. This constructor will be called when the object is being created. 

Example:  In this example, we are defining a constructor which takes a name as a parameter. So, at the time of the creation of an object p1, we are passing the only single argument – name. We do not require brackets to write parameters, that is the simplicity of CoffeeScript. When class Person is instantiated, the constructor will be invoked, and the print statement inside it will be executed and print “Hello, Sam” in the console. 

Javascript




class Person
  constructor: (@name) ->
      console.log "Hello, " + @name 
  
p = new Person "Sam"


Output:

Hello, Sam

Example: What if the constructor requires multiple arguments. In the below example, we are passing multiple values as arguments separated by a comma while invoking the class – name and age.

Javascript




class MyClass
   constructor: (@name,@age) ->
       console.log @name + " is #{@age} years old"
     
p1 = new MyClass "Sam", "12"


Output:

Sam is 12 years old

Modify Variables of a Class: We can only modify the public accessible variables of a class. If a data member is private, we can’t access it and cannot modify it. In the below code, we have public variables only -name and age. We are storing the values of name and age in variables – names and ages. Let’s see if we can modify the value of any outside the class. 

Javascript




class MyClass
    constructor: (@name,@age) ->
        names = @name
        ages = @age
          
    func: ->
        console.log @name + " is #{@age} years old."
         
     
p1 = new MyClass "Sam","10"
p1.func()
p1.age = '3'
p1.func()


In this example, first, we invoked function on object p1, and then we are modifying the value of age to 3 by accessing age through object p1, and again invoked the function. See the output below, the value of age is changed. This is the way you can change the values of class public data members outside the class.

Output: 

Sam is 10 years old.
Sam is 3 years old.

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!