JavaScript Object Methods
Learn how to add and use functions inside objects
⚡ What are Object Methods?
Methods are functions stored as object properties. They allow objects to perform actions and behaviors.
let person = {
name: "Alice",
greet: function() {
return "Hello, I'm " + this.name;
}
};
Types of Object Methods
Function Expression
Traditional method definition
greet: function() {
return "Hello!";
}
Method Shorthand
ES6 simplified syntax
greet() {
return "Hello!";
}
Arrow Functions
Concise function syntax
greet: () => {
return "Hello!";
}
External Functions
Assign existing functions
function sayHello() {
return "Hello!";
}
obj.greet = sayHello;
🔹 Basic Object Methods
Here's how to define and use methods in objects:
let calculator = {
x: 10,
y: 5,
// Method using function expression
add: function() {
return this.x + this.y;
},
// Method using ES6 shorthand
subtract() {
return this.x - this.y;
},
// Method with parameters
multiply(a, b) {
return a * b;
}
};
console.log(calculator.add()); // 15
console.log(calculator.subtract()); // 5
console.log(calculator.multiply(3, 4)); // 12
Console Output:
15
5
12
🔹 Methods with 'this' Keyword
Use 'this' to access other properties within the same object:
let person = {
firstName: "John",
lastName: "Doe",
age: 25,
// Method that uses 'this'
getFullName() {
return this.firstName + " " + this.lastName;
},
// Method that modifies properties
haveBirthday() {
this.age++;
return "Happy birthday! Now " + this.age + " years old.";
},
// Method with parameters
introduce(greeting) {
return greeting + ", I'm " + this.getFullName();
}
};
console.log(person.getFullName()); // "John Doe"
console.log(person.haveBirthday()); // "Happy birthday! Now 26 years old."
console.log(person.introduce("Hi")); // "Hi, I'm John Doe"
Console Output:
John Doe
Happy birthday! Now 26 years old.
Hi, I'm John Doe
🔹 Adding Methods After Creation
You can add methods to existing objects:
let car = {
brand: "Toyota",
model: "Camry",
year: 2022
};
// Add method using dot notation
car.getInfo = function() {
return this.year + " " + this.brand + " " + this.model;
};
// Add method using bracket notation
car["start"] = function() {
return "The " + this.brand + " is starting...";
};
console.log(car.getInfo()); // "2022 Toyota Camry"
console.log(car.start()); // "The Toyota is starting..."
Console Output:
2022 Toyota Camry
The Toyota is starting...
🔹 Methods vs Functions
Understanding the difference between methods and regular functions:
// Regular function
function regularFunction() {
return "I'm a regular function";
}
let myObject = {
name: "Test Object",
// Method - function inside an object
myMethod() {
return "I'm a method of " + this.name;
},
// Method calling another method
callMyMethod() {
return this.myMethod() + " - called from another method";
}
};
console.log(regularFunction()); // "I'm a regular function"
console.log(myObject.myMethod()); // "I'm a method of Test Object"
console.log(myObject.callMyMethod()); // "I'm a method of Test Object - called from another method"
Console Output:
I'm a regular function
I'm a method of Test Object
I'm a method of Test Object - called from another method
🔹 Real-World Example
A practical example of an object with multiple methods:
let bankAccount = {
accountNumber: "12345",
balance: 1000,
// Method to check balance
checkBalance() {
return "Current balance: $" + this.balance;
},
// Method to deposit money
deposit(amount) {
if (amount > 0) {
this.balance += amount;
return "Deposited $" + amount + ". " + this.checkBalance();
}
return "Invalid deposit amount";
},
// Method to withdraw money
withdraw(amount) {
if (amount > 0 && amount <= this.balance) {
this.balance -= amount;
return "Withdrew $" + amount + ". " + this.checkBalance();
}
return "Invalid withdrawal amount or insufficient funds";
}
};
console.log(bankAccount.checkBalance()); // "Current balance: $1000"
console.log(bankAccount.deposit(500)); // "Deposited $500. Current balance: $1500"
console.log(bankAccount.withdraw(200)); // "Withdrew $200. Current balance: $1300"
Console Output:
Current balance: $1000
Deposited $500. Current balance: $1500
Withdrew $200. Current balance: $1300