Photo by Mohammad Rahmani on Unsplash
Discover How JavaScript Handles Object Properties and Methods
How JavaScript Deals with Object Properties and Methods
JavaScript objects form the backbone of programming, encapsulating data and behavior. They consist of properties (attributes) and methods (functions).
This article will walk you through examples, use cases, and best practices for working with objects in JavaScript.
Properties
Properties define the state or attributes of an object. They associate a unique key (property name) with a value. Let’s explore examples to understand properties better.
Example 1: Basic Object Properties
let calculator = {
add: function(a, b) {
return a + b;
},
multiply: function(a, b) {
return a * b;
}
};
console.log(calculator.add(2, 3)); // Output: 5
console.log(calculator.multiply(4, 5)); // Output: 20
Example 2: Using Dynamic Keys
Dynamic keys enable flexibility in defining object properties.
let property = "color";
let item = {
name: "Chair",
[property]: "blue"
};
console.log(item.color); // Output: blue
Example 3: Nested Properties
Properties can be nested within objects to represent hierarchical data.
let student = {
name: "Alice",
scores: {
math: 85,
science: 92
}
};
console.log(student.scores.math); // Output: 85
Use Cases for Properties
Representing Data: Storing attributes like
name
,age
, orpreferences
.Configuring Settings: Defining app settings, e.g.,
theme: "dark"
.
Methods
Methods define the behavior or actions of an object. They are functions associated with an object.
Example 1: Object with Methods
let calculator = {
add: function(a, b) {
return a + b;
},
multiply: function(a, b) {
return a * b;
}
};
console.log(calculator.add(2, 3)); // Output: 5
console.log(calculator.multiply(4, 5)); // Output: 20
Example 2: Accessing Properties in Methods
Methods can access properties of their own object using this
.
let car = {
brand: "Tesla",
speed: 120,
getDetails: function() {
return `Brand: ${this.brand}, Speed: ${this.speed}`;
}
};
console.log(car.getDetails()); // Output: Brand: Tesla, Speed: 120
Example 3: Chaining Methods
Chaining methods allows multiple actions in sequence.
let counter = {
value: 0,
increment: function() {
this.value++;
return this;
},
decrement: function() {
this.value--;
return this;
},
show: function() {
console.log(this.value);
return this;
}
};
counter.increment().increment().decrement().show(); // Output: 1
Use Cases for Methods
Encapsulating Behavior: Actions like
.start()
or.calculate()
.Interactive UI: Toggling visibility with
.toggleVisibility()
.APIs: Custom methods like
.fetchData()
for HTTP requests.
Combining Properties and Methods
Using properties and methods together makes objects powerful and versatile.
Example: Bank Account Management
let bankAccount = {
accountHolder: "Sarah",
balance: 1000,
deposit: function(amount) {
this.balance += amount;
return `New balance: ${this.balance}`;
},
withdraw: function(amount) {
if (amount > this.balance) {
return "Insufficient funds";
} else {
this.balance -= amount;
return `New balance: ${this.balance}`;
}
}
};
console.log(bankAccount.deposit(500)); // Output: New balance: 1500
console.log(bankAccount.withdraw(200)); // Output: New balance: 1300
Key Points to Remember
Properties describe the state or attributes of an object.
Methods define the functionality or actions related to the object.
Use
this
in methods to refer to the object itself.
So, in the end, highlighting their key components, Properties store the state or attributes of an object, while methods define the actions an object can perform.
Through clear examples, it explains basic object properties, dynamic keys, nested properties, and the use of this
in methods. Advanced techniques like method chaining and combining properties with methods are also covered, demonstrating practical applications such as managing a bank account.