जावास्क्रिप्ट में किसी अन्य वस्तु के माध्यम से किसी वस्तु तक पहुँचने के लिए कोड निम्नलिखित है -
उदाहरण
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <style> body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } .result { font-size: 18px; font-weight: 500; color: rebeccapurple; } </style> </head> <body> <h1>Access an object through another object in JavaScript</h1> <div class="result"></div> <button class="Btn">CLICK HERE</button> <h3>Click on the above button to display obj2</h3> <script> let resEle = document.querySelector(".result"); let BtnEle = document.querySelector(".Btn"); let obj = { firstName: "Rohan", lastName: "Sharma", welcome() { return "Welcome " + this.firstName + " " + this.lastName; }, }; let obj2 = { firstName: "Mohit", lastName: obj.lastName, welcomeString: obj.welcome(), }; BtnEle.addEventListener("click", () => { resEle.innerHTML = "obj2.firstName = " + obj2.firstName + "<br>"; resEle.innerHTML += "obj2.lastName = " + obj2.lastName + "<br>"; resEle.innerHTML += "obj2.welcomeString = " + obj2.welcomeString + "<br>"; }); </script> </body> </html>
आउटपुट
उपरोक्त कोड निम्न आउटपुट उत्पन्न करेगा -
'यहां क्लिक करें' बटन पर क्लिक करने पर -