get कीवर्ड का उपयोग गेटर फंक्शन के रूप में किया जा सकता है जैसे C#, Java और अन्य टेक्नोलॉजी में।
हम क्लास में get के साथ फंक्शन को निम्नानुसार सेट करते हैं:
class Employee {
constructor(name) {
this.name = name;
}
get fullName() {
return this.name;
}
}
उदाहरण
get का उदाहरण प्रदर्शित करने वाला निम्नलिखित कोड है:
class Employee {
constructor(name) {
this.name = name;
}
get fullName() {
return this.name;
}
}
var employeeObject = new Employee("David Miller");
console.log(employeeObject.fullName);
उपरोक्त प्रोग्राम को चलाने के लिए, आपको निम्नलिखित कमांड का उपयोग करना होगा:
node fileName.js
यहां, मेरा फ़ाइल नाम demo299.js है।
आउटपुट
यह कंसोल पर निम्नलिखिक आउटपुट देगा:
PS C:\Users\Amit\javascript-code> node demo299.js David Miller