๐ User Defined Objects in JavaScript
(Creating Objects, Instances, Objects within Objects)
๐ง 1. Introduction to User Defined Objects
In JavaScript, a User Defined Object is an object created by the programmer to store and manage related data and behavior.
๐ Objects consist of:
- Properties (data)
- Methods (functions)
โ Example:
let student = {
name: "Rahul",
age: 22,
greet: function() {
console.log("Hello " + this.name);
}
};
student.greet();
๐งฉ 2. Creating a User Defined Object
There are multiple ways to create objects:
(1) Object Literal (Most Common)
let person = {
name: "Aman",
age: 25,
city: "Delhi"
};
โ Advantages:
- Simple and quick
- Easy to read
(2) Using new Object()
let person = new Object();
person.name = "Aman";
person.age = 25;
(3) Using Constructor Function
function Person(name, age) {
this.name = name;
this.age = age;
}
let p1 = new Person("Rahul", 22);
let p2 = new Person("Aman", 25);
๐ This is used to create multiple similar objects.
(4) Using ES6 Class
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
}
let p1 = new Person("Rahul", 22);
๐งฌ 3. Instances of Objects
An instance is a specific object created from a constructor or class.
๐น Example:
function Car(model) {
this.model = model;
}
let c1 = new Car("BMW"); // Instance 1
let c2 = new Car("Audi"); // Instance 2
๐ Key Points:
- Each instance has its own data
- Instances share structure (same constructor)
๐ 4. Adding Properties and Methods Dynamically
Objects in JavaScript are dynamic.
let user = {};
user.name = "John";
user.age = 30;
user.greet = function() {
console.log("Hi " + this.name);
};
๐งฉ 5. Objects within Objects (Nested Objects)
JavaScript allows objects to contain other objects.
๐น Example:
let student = {
name: "Rahul",
address: {
city: "Delhi",
pincode: 110001
}
};
console.log(student.address.city);
๐น Complex Example:
let company = {
name: "TechCorp",
employee: {
name: "Aman",
skills: {
primary: "JavaScript",
secondary: "Python"
}
}
};
console.log(company.employee.skills.primary);
๐ 6. Accessing Object Properties
(a) Dot Notation
console.log(student.name);
(b) Bracket Notation
console.log(student["name"]);
๐ 7. Iterating Through Object
for (let key in student) {
console.log(key + ": " + student[key]);
}
๐ 8. Complete Example
function Student(name, age) {
this.name = name;
this.age = age;
this.display = function() {
console.log(this.name + " - " + this.age);
};
}
let s1 = new Student("Rahul", 22);
let s2 = new Student("Aman", 25);
s1.display();
s2.display();
๐ Summary Table
| Concept | Description |
|---|---|
| User Defined Object | Custom object created by user |
| Object Literal | Simple object creation |
| Constructor | Create multiple objects |
| Instance | Object created from constructor |
| Nested Object | Object inside object |
โ ๏ธ Important Points
- Objects are dynamic and flexible
- Use constructor or class for multiple objects
- Nested objects help represent real-world structures
๐ฏ Conclusion
User Defined Objects are essential for:
- Structuring data
- Creating reusable components
- Building real-world applications
๐ They are a core concept in object-oriented programming in JavaScript and very important for MCA exams.
