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
How to define methods for an Object in JavaScript?
Methods are functions that let an object perform actions or have actions performed on it. The key difference between a function and a method is that a function is a standalone unit of statements, while a method is attached to an object and can be referenced using the this keyword.
Methods are used for everything from displaying object contents to performing complex mathematical operations on properties and parameters.
Method 1: Defining Methods During Object Creation
You can define methods directly when creating an object using object literal notation:
<script>
let person = {
name: "John",
age: 30,
greet: function() {
return "Hello, my name is " + this.name;
},
getAge: function() {
return "I am " + this.age + " years old";
}
};
document.write("<p>" + person.greet() + "</p>");
document.write("<p>" + person.getAge() + "</p>");
</script>
Hello, my name is John I am 30 years old
Method 2: Adding Methods After Object Creation
You can add methods to an existing object by assigning functions to new properties:
<script>
let car = {
brand: "Toyota",
model: "Camry"
};
// Adding methods after object creation
car.start = function() {
return this.brand + " " + this.model + " is starting...";
};
car.getInfo = function() {
return "Brand: " + this.brand + ", Model: " + this.model;
};
document.write("<p>" + car.start() + "</p>");
document.write("<p>" + car.getInfo() + "</p>");
</script>
Toyota Camry is starting... Brand: Toyota, Model: Camry
Method 3: Using Constructor Functions
You can define methods inside constructor functions to create multiple objects with the same methods:
<script>
function Student(name, grade) {
this.name = name;
this.grade = grade;
this.study = function() {
return this.name + " is studying hard!";
};
this.getGrade = function() {
return this.name + " has grade: " + this.grade;
};
}
let student1 = new Student("Alice", "A");
let student2 = new Student("Bob", "B+");
document.write("<p>" + student1.study() + "</p>");
document.write("<p>" + student2.getGrade() + "</p>");
</script>
Alice is studying hard! Bob has grade: B+
Method 4: ES6 Shorthand Method Syntax
Modern JavaScript allows you to define methods using shorthand syntax, omitting the function keyword:
<script>
let calculator = {
value: 0,
add(num) {
this.value += num;
return this;
},
multiply(num) {
this.value *= num;
return this;
},
getValue() {
return this.value;
}
};
calculator.add(5).multiply(3);
document.write("<p>Result: " + calculator.getValue() + "</p>");
</script>
Result: 15
Comparison
| Method | When to Use | Advantages |
|---|---|---|
| Object Literal | Single objects | Simple and direct |
| Adding After Creation | Extending existing objects | Flexible modification |
| Constructor Function | Multiple similar objects | Reusable pattern |
| ES6 Shorthand | Modern code | Cleaner syntax |
Conclusion
JavaScript provides multiple ways to define object methods. Choose object literals for simple cases, constructor functions for reusable patterns, and ES6 shorthand for modern, clean syntax.
