Skip to content
Home ยป User Defined Objects

User Defined Objects

๐ŸŒ 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

ConceptDescription
User Defined ObjectCustom object created by user
Object LiteralSimple object creation
ConstructorCreate multiple objects
InstanceObject created from constructor
Nested ObjectObject 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.