गेट्टर फंक्शन बनाने के लिए गेट कीवर्ड का उपयोग करें।
उदाहरण
निम्नलिखित कोड है -
const studentDetails =
{
studentName: "David Miller",
get studentName() {
console.log('I am calling the getter method...')
}
}
console.log(studentDetails.studentName); उपरोक्त प्रोग्राम को चलाने के लिए, आपको निम्न कमांड का उपयोग करने की आवश्यकता है -
आउटपुट इस प्रकार है -
node fileName.js.
यहाँ, मेरी फ़ाइल का नाम है demo221.js.
आउटपुट
आउटपुट इस प्रकार है -
PS C:\Users\Amit\JavaScript-code> node demo221.js I am calling the getter method... Undefined