गेटर फ़ंक्शन बनाने के लिए get कीवर्ड का उपयोग करें।
उदाहरण
निम्नलिखित कोड देखें:
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