π§ Objects in JavaScript β A Simple and Complete Guide
After understanding functions as workers, the next important concept is objects.
If functions are workers, then:
Objects are like real-world things that store data and behavior together.
π± What is an Object? (First Principle)
An object is like a person or thing that has:
- properties (data)
- actions (functions)
π§ Real-Life Analogy
Think of a tea shop owner:
- Name β Arun
- Age β 25
- Job β Making tea
And he can:
- make tea
- take orders
π That is exactly what an object is.
βοΈ Basic Definition
An object is a collection of key-value pairs, where values can be data or functions.
β Simple Example
```javascript id="k0f8kp"
const teaShop = {
owner: "Arun",
location: "Erode",
makeTea: function() {
console.log("Tea is ready");
}
};
---
π Here:
* `owner`, `location` β properties
* `makeTea` β function (method)
---
# π¦ Accessing Object Data
```javascript id="3zv2n5"
console.log(teaShop.owner); // Arun
teaShop.makeTea(); // Tea is ready
π§ Objects + Functions (Important Connection)
When a function is inside an object, it is called a method.
```javascript id="k2p4zj"
const user = {
name: "Jegan",
greet() {
console.log("Hello " + this.name);
}
};
---
π `this.name` refers to the object itself.
---
# π The `this` Keyword
> `this` means βthe current objectβ
```javascript id="9f4gq2"
const person = {
name: "Arun",
speak() {
console.log(this.name);
}
};
β this = person object
π§Ύ Creating Objects (Different Ways)
1. Object Literal (Most Common)
```javascript id="3xvmbu"
const car = {
brand: "Toyota",
start() {
console.log("Car started");
}
};
---
## 2. Constructor Function
```javascript id="0m9z1p"
function Car(brand) {
this.brand = brand;
}
const c1 = new Car("Honda");
3. Using class (Modern Way)
```javascript id="p5yd4o"
class Car {
constructor(brand) {
this.brand = brand;
}
}
---
# π Objects are Dynamic
You can add or change properties anytime.
```javascript id="0z9xxf"
const user = {};
user.name = "Jegan";
user.age = 22;
π§ Objects are Reference Types
```javascript id="mbg1tx"
const a = { name: "A" };
const b = a;
b.name = "B";
console.log(a.name); // B
π Both point to the same object in memory.
---
# π§ͺ Useful Object Methods
---
## Object.keys()
```javascript id="y4whbq"
Object.keys(teaShop);
Object.values()
```javascript id="y5o0o1"
Object.values(teaShop);
---
## Object.entries()
```javascript id="8tvjv2"
Object.entries(teaShop);
π Real-World Example
User Object (CRM App)
```javascript id="mtu3kk"
const user = {
id: 1,
name: "Jegan",
email: "jegan@gmail.com",
login() {
console.log("User logged in");
}
};
---
## Transaction Object
```javascript id="4k2b7x"
const transaction = {
amount: 5000,
type: "expense",
category: "rent"
};
π Objects + Functions Together
Objects store data
Functions operate on data
Together, they build applications.
π§ Final Understanding
An object is a real-world entity in your program that groups related data and behavior together.
π§ Simple Definition (Child Level)
An object is like a person or thing that has information and can do actions.
π Conclusion
Objects help you:
- Organize data
- Represent real-world things
- Build structured applications
π In short:
Objects are containers of data and behavior in JavaScript.
If functions are workers,
then objects are people or things those workers belong to.
Top comments (0)