Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Another method other than a constructor in a JavaScript class?
JavaScript classes support multiple types of methods beyond constructors. While constructors initialize objects, other methods provide functionality and behavior to class instances.
The constructor is used to create and initialize an instance of a class. However, classes can contain additional methods that perform specific operations, calculations, or return formatted data. These methods are called on class instances and have access to the object's properties through the this keyword.
Types of Methods in JavaScript Classes
JavaScript classes support several method types:
- Instance methods: Called on class instances
- Static methods: Called on the class itself
- Getter/Setter methods: Control property access
Syntax
class ClassName {
constructor(parameters) {
// Initialize properties
}
methodName() {
// Instance method
return someValue;
}
static staticMethod() {
// Static method
}
}
Example 1: Instance Method
This example demonstrates a class with an instance method that returns formatted information:
<!DOCTYPE html>
<html>
<head>
<title>Instance Methods in JavaScript Classes</title>
</head>
<body>
<p id="method"></p>
<script>
class Person {
constructor(name) {
this.name = name;
}
getPersonName() {
return 'The person name is: ' + this.name;
}
greet() {
return 'Hello, I am ' + this.name;
}
}
const person1 = new Person("Raghu");
document.getElementById("method").innerHTML =
person1.getPersonName() + "<br>" + person1.greet();
</script>
</body>
</html>
The person name is: Raghu Hello, I am Raghu
Example 2: Multiple Instance Methods
This example shows a class with multiple methods that process and return different information:
<!DOCTYPE html>
<html>
<head>
<title>Multiple Methods in JavaScript Classes</title>
</head>
<body>
<p id="method"></p>
<script>
class Employee {
constructor(name, id, designation) {
this.name = name;
this.id = id;
this.designation = designation;
}
getEmployeeDetails() {
return this.name + " has the designation: " + this.designation;
}
getEmployeeId() {
return "Employee ID: " + this.id;
}
}
const employee1 = new Employee("Rajesh", "10023", "Software Engineer");
document.getElementById("method").innerHTML =
employee1.getEmployeeDetails() + "<br>" + employee1.getEmployeeId();
</script>
</body>
</html>
Rajesh has the designation: Software Engineer Employee ID: 10023
Example 3: Methods with Parameters
This example demonstrates methods that accept parameters and use them along with instance properties:
<!DOCTYPE html>
<html>
<head>
<title>Methods with Parameters</title>
</head>
<body>
<p id="method"></p>
<script>
class Company {
constructor(branch) {
this.name = branch;
}
getLeaderInfo(leader) {
return leader + " is the head of " + this.name;
}
getBranchInfo() {
return "Branch: " + this.name;
}
}
const company = new Company("Microsoft");
document.getElementById("method").innerHTML =
company.getLeaderInfo("Bill Gates") + "<br>" + company.getBranchInfo();
</script>
</body>
</html>
Bill Gates is the head of Microsoft Branch: Microsoft
Key Points
- Instance methods are called on objects created from the class
- Methods have access to instance properties via
this - Methods can accept parameters and return values
- Multiple methods can be defined in a single class
Conclusion
JavaScript classes support various method types beyond constructors. Instance methods provide functionality to class objects and can accept parameters, process data, and return formatted results, making classes powerful tools for object-oriented programming.
