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.
Similar Reads
CoffeeScript Class Method
Methods: Methods are variable functions defined inside the class. Methods are the behavior of objects, they describe the properties of an object, and can modify the state of the object. Objects can also contain methods. An object has its attributes and methods. We can create many instances of a clas
3 min read
TypeScript class
A TypeScript class is a blueprint for creating objects, encapsulating properties (data) and methods (behavior) to promote organization, reusability, and readability. Supports inheritance, allowing one class to extend another and reuse functionality.Provides access modifiers (public, private, protect
4 min read
JavaScript Classes
JavaScript classes (introduced in ES6) provide a structured way to create objects with shared properties and methods. They support inheritance, encapsulation, and modularity, making it easier to write object-oriented code. Syntax class ClassName { constructor() { // Initialize properties here } // D
4 min read
Explain Class Methods in Coffeescript
CoffeeScript is a lightweight language that compiles into JavaScript. As compared to JavaScript, it provides simple and easy-to-learn syntax avoiding the complex syntax of JavaScript. CoffeeScript is influenced by languages such as JavaScript, YAML, Ruby, and Python and has also influenced languages
2 min read
Typescript Abstract Class
TypeScript, a superset of JavaScript, introduces many features to improve code organization, readability, and maintainability. One of these features is abstract classes. This article will explain what abstract classes are, how they work, and how to use them effectively in your TypeScript projects. T
3 min read
CoffeeScript | Statements
The syntax of CoffeeScript is simpler to JavaScript and can be easily learned if you have knowledge of JavaScript. It avoids the use of semicolons, curly braces, and variable declarations. CoffeeScript Statements: The statements of CoffeeScript do not end with semicolons (;). In this language, a new
3 min read
CoffeeScript Installation
CoffeeScript is a lightweight language that compiles into JavaScript. It provides simple and easy-to-learn syntax, avoiding the complex syntax of JavaScript. CoffeeScript is influenced by JavaScript, Ruby, YAML, Haskell, Perl, and Python and has influenced Moon Script, Live Script, and JavaScript. I
2 min read
CoffeeScript | Introduction
CoffeeScript is a lightweight language that compiles JavaScript. It provides simple and easy-to-learn syntax avoiding the complex syntax of JavaScript. CoffeeScript is influenced by JavaScript, Ruby, YAML, Haskell, Perl, and Python and has influenced MoonScript, LiveScript, and JavaScript. In this t
4 min read
ES6 Classes
There are three concepts in Object-Oriented Programming Object, Class, and Methods. ES6 JavaScript supports Object-Oriented programming components. Object: A real-time object entity means the presentation of any entity in real-time.Class: It is the before the plan of creating any objects which is kn
3 min read
PHP Classes
A class defines the structure of an object. It contains properties (variables) and methods (functions). These properties and methods define the behavior and characteristics of an object created from the class. Syntax: <?phpclass Camera { // code goes here...}?>Now, let us understand with the h
2 min read