new ऑपरेटर का उपयोग यूजर-डिफ़ाइंड ऑब्जेक्ट टाइप के इंस्टेंस या बिल्ट-इन ऑब्जेक्ट टाइप्स में से एक के इंस्टेंस को बनाने के लिए किया जाता है जिसमें कंस्ट्रक्टर फ़ंक्शन होता है।
जावास्क्रिप्ट में new ऑपरेटर का कोड निम्नलिखित है -
उदाहरण
<!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: 20px; font-weight: 500; color: blueviolet; } </style> </head> <body> <h1>जावास्क्रिप्ट में New ऑपरेटर</h1> <div class="result"></div> <br /> <button class="Btn">यहाँ क्लिक करें</button> <h3>Person कंस्ट्रक्टर से person1 ऑब्जेक्ट बनाने के लिए ऊपर दिए गए बटन पर क्लिक करें</h3> <script> let resEle = document.querySelector(".result"); let BtnEle = document.querySelector(".Btn"); function Person(name, age, designation) { this.name = name; this.age = age; this.designation = designation; } let person1 = new Person("Rohan Sharma", 19, "student"); BtnEle.addEventListener("click", () => { resEle.innerHTML = "person1.name = " + person1.name + "<br>"; resEle.innerHTML += "person1.age = " + person1.age + "<br>"; resEle.innerHTML += "person1.designation = " + person1.designation + "<br>"; }); </script> </body> </html>
आउटपुट

'यहाँ क्लिक करें' बटन पर क्लिक करने पर -
