Explain Class Methods in Coffeescript


To understand the class methods in CoffeeScript first we need to understand what it is CoffeeScript. CoffeeScript is a lightweight programming language, and this language compiles in JavaScript and is also inspired by languages like JavaScript, python, ruby, Haskell, etc. Further, we will discuss it in detail after that we are going to discuss class methods in CoffeeScript with its syntax of it an example with a full explanation and in the end, we conclude it.

Introduction of CoffeeScript

As we discussed above it is a lightweight programming language that compiles in JavaScript. It is basically an effort to make JavaScript more concise and readable influenced by python, Ruby, Haskell, etc.

Advantages of the CoffeeScript

  • The syntax of this language is comparable to JavaScript's. The simplicity of the syntax is what gives this programming language its charm. CoffeeScript is an extremely readable and straightforward programming language.

  • The var keyword is unnecessary before declaring a variable, in contrast to JavaScript. As a result, it helps prevent scope declaration issues in software.

  • Semicolons, parentheses, and curly brackets all serve no purpose in CoffeeScript. Instead of these, whitespaces are used to distinguish the code within functions, loops, etc.

  • Compared to JavaScript, there are half as many lines of code. Benefit: Software becomes simpler and easier to understand when there is less code.

  • CoffeeScript provides the concept of aliases for a number of operators, resulting in clear and readable code. Programming is made simple with CoffeeScript.

  • CoffeeScript is a stable and safe programming language for building dynamic web pages.

Class Methods in CoffeeScript

Class methods are nothing but Methods are the variables that are defined inside the class. Methods can change an object's state, explain an object's characteristics, and act as the behaviour of the object. Methods can also be found in objects.

The internal variable operations of the class are referred to as methods. Methods have the power to alter an item's state, describe its properties, and simulate the behaviour of the object. Objects contain methods as well. The characteristics and operations of an object. There may be numerous instances of a class, and in each case, the class properties will be set to the appropriate value.

Methods come in two types: those with arguments and those without. When invoking a function that doesn't require parameters, we don't need to be concerned about it. Let's examine the two categories of methods defined within the class and how they function.

Methods without Parameters

If there is no need for outside data at the time of calling then the function can be created which doesn’t have any parameters. These types of functions or methods can be used when the user needs to get output either on the basis of just global variables or the data present inside the method. Let’s see an example to get more understanding of the concept.

Example

In this example, we will create a class and inside the class we will define the method which will not take any parameter.

class class_name
   constructor: (@first_name,@last_name) ->
      console.log "Name of person is: " + @first_name + " " + @last_name 
         
   first_Name: ->
      console.log "First name of person is: " + @first_name
    last_Name: -> 
      console.log "Last name of person is: " + @last_name
     
person1 = new class_name "Sam", "Sidhu"
person1.first_Name()
person1.last_Name()

On compiling, the above CoffeeScript produces the following JavaScript −

// Generated by CoffeeScript 2.4.1
(function() {
   var class_name, person1;
   class_name = class class_name {
      constructor(first_name, last_name) {
         this.first_name = first_name;
         this.last_name = last_name;
         console.log("Name of person is: " + this.first_name + " " +
         this.last_name);
      }
      first_Name() {
         return console.log("First name of person is: " + this.first_name);
      }
      last_Name() {
         return console.log("Last name of person is: " + this.last_name);
      }
   };
   person1 = new class_name("Sam", "Sidhu");
   person1.first_Name();
   person1.last_Name();
}).call(this);

In the above code, first we have create a class with name ‘class_name’ and then in the class we have defined the constructor which will initialize the object of the class. In the class, we have defined two methods which will print the first and the last name of the object that will be person for this class and they will not take any parameter as we can see in the code and result in the output.

Methods with Parameters

Most of the time functions are used to perform some calculations or the results and then based on the computations the functions will provide the output. Also, to perform dynamically its best way for the functions to take some outer data in the form of the parameters. Also functions or methods with the parameters can work on the global data or the data present in the outer scope like data of an object. Let’s see an example to get more understanding of the concept.

Example

In this example, we will create a class and inside the class we will define the method which will take some parameters and based on the parameters will provides the output −

class class_name
   constructor: (@first_name,@last_name) ->
      console.log "Name of person is: " + @first_name + " " + @last_name
   age: (age) ->
      console.log "Age of : " + @first_name + " " + @last_name + " is:" + age
   first_Name: ->
      console.log "First name of person is: " + @first_name
   last_Name: ->
      console.log "Last name of person is: " + @last_name
person1 = new class_name "Sam", "Sidhu"
person1.first_Name()
person1.last_Name()
person1.age(22)

On compiling, the above CoffeeScript produces the following JavaScript −

// Generated by CoffeeScript 2.4.1
(function() {
   var class_name, person1;
   class_name = class class_name {
      constructor(first_name, last_name) {
         this.first_name = first_name;
         this.last_name = last_name;
         console.log("Name of person is: " + this.first_name + " " +
         this.last_name);
      }
      age(age) {
         return console.log("Age of : " + this.first_name + " " +
         this.last_name + " is: " + age);
      }
      first_Name() {
         return console.log("First name of person is: " + this.first_name);
      }
      last_Name() {
         return console.log("Last name of person is: " + this.last_name);
      }
   };
   person1 = new class_name("Sam", "Sidhu");
   person1.first_Name();
   person1.last_Name();
   person1.age(22);
}).call(this);

In the above code, first we have create a class with name ‘class_name’ and then in the class we have defined the constructor which will initialize the object of the class. In the class, we have defined two methods which will print the first and the last name of the object that will be person for this class and they will not take any parameter but there is third method present which will take a parameter and that function will use both the data from parameter as well as the scope of the class.

Conclusion

In this article we have learned that CoffeeScript is a lightweight programming language, and this language compiles in JavaScript and is also inspired by languages like JavaScript, python, ruby, Haskell, etc. Class methods are nothing but Methods are the variables that are defined inside the class. Methods can change an object's state, explain an object's characteristics, and act as the behaviour of the object. Methods can also be found in objects. There are two types of methods one that takes the parameters and other that doesn’t take any parameter.

Updated on: 04-Apr-2023

71 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements