Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
How to access an object through another object in JavaScript?
In JavaScript, you can access properties and methods of one object from another object by referencing them directly. This technique is useful for sharing data between objects or creating relationships.
Basic Property Access
The simplest way is to reference properties directly from another object:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Object Access Example</title>
</head>
<body>
<button id="showBtn">CLICK HERE</button>
<script>
// First object with properties and method
let obj = {
firstName: "Rohan",
lastName: "Sharma",
welcome() {
return "Welcome " + this.firstName + " " + this.lastName;
}
};
// Second object accessing first object's properties
let obj2 = {
firstName: "Mohit",
lastName: obj.lastName, // Direct property access
welcomeString: obj.welcome(), // Method call result
originalObj: obj // Store reference to entire object
};
document.getElementById("showBtn").addEventListener("click", () => {
let output = `
obj2.firstName = ${obj2.firstName}<br>
obj2.lastName = ${obj2.lastName}<br>
obj2.welcomeString = ${obj2.welcomeString}<br>
Original object name: ${obj2.originalObj.firstName}
`;
document.getElementById("result").innerHTML = output;
});
</script>
</body>
</html>
Dynamic Property Access
You can also access object properties dynamically using bracket notation:
let person = {
name: "Alice",
age: 25,
city: "New York"
};
let employee = {
id: 101,
department: "IT",
// Access person's properties dynamically
getPersonInfo: function(property) {
return person[property];
}
};
console.log(employee.getPersonInfo("name")); // Alice
console.log(employee.getPersonInfo("age")); // 25
console.log(employee.getPersonInfo("city")); // New York
Alice 25 New York
Method Borrowing with call()
You can borrow methods from one object and use them with another object's context:
let person1 = {
firstName: "John",
lastName: "Doe",
getFullName: function() {
return this.firstName + " " + this.lastName;
}
};
let person2 = {
firstName: "Jane",
lastName: "Smith"
};
// Borrow person1's method for person2
let person2Name = person1.getFullName.call(person2);
console.log(person2Name); // Jane Smith
// Store the borrowed method
person2.getFullName = person1.getFullName;
console.log(person2.getFullName()); // Jane Smith
Jane Smith Jane Smith
Comparison of Access Methods
| Method | Use Case | Dynamic? |
|---|---|---|
Direct property access (obj.property) |
Known property names | No |
Bracket notation (obj[property]) |
Dynamic property names | Yes |
Method borrowing (call/apply) |
Using methods with different context | Yes |
Conclusion
Accessing objects through other objects enables data sharing and method reuse. Use direct property access for simple cases, bracket notation for dynamic access, and method borrowing when you need to change the execution context.
Advertisements
