जावास्क्रिप्ट दिनांक प्रोटोटाइप
उदाहरण
एक नई तिथि विधि बनाएं जो दिनांक वस्तु को एक महीने के नाम की संपत्ति देता है जिसे myName कहा जाता है:
Date.prototype.myMonth = function()
{
if (this.getMonth()==0) {return "January"};
if (this.getMonth()==1) {return "February"};
if (this.getMonth()==2) {return "March"};
if (this.getMonth()==3) {return "April"};
if (this.getMonth()==4) {return "May"};
if (this.getMonth()==5) {return "June"};
if (this.getMonth()==6) {return "July"};
if (this.getMonth()==7) {return "August"};
if (this.getMonth()==8) {return "September"};
if (this.getMonth()==9) {return "October"};
if (this.getMonth()==10) {return "November"};
if (this.getMonth()==11) {return "December"};
}
दिनांक वस्तु बनाएं, फिर myMonth विधि को कॉल करें:
const d = new Date();
let month = d.myMonth();
परिभाषा और उपयोग
prototype
आपको तिथियों में नए गुण और विधियाँ जोड़ने की अनुमति देता है।
prototype
एक संपत्ति है जो सभी जावास्क्रिप्ट वस्तुओं के साथ उपलब्ध है।
ब्राउज़र समर्थन
prototype
एक ECMAScript1 (ES1) सुविधा है।
ES1 (जावास्क्रिप्ट 1997) सभी ब्राउज़रों में पूरी तरह से समर्थित है:
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | Yes | Yes | Yes | Yes | Yes |
वाक्य - विन्यास
Date.prototype.name = value
चेतावनी
आपको किसी ऐसी वस्तु के प्रोटोटाइप को बदलने की सलाह नहीं दी जाती है जिसे आप नियंत्रित नहीं करते हैं।
आपको जावास्क्रिप्ट डेटाटाइप में निर्मित प्रोटोटाइप को नहीं बदलना चाहिए जैसे:
- नंबर
- स्ट्रिंग्स
- सरणियों
- खजूर
- बूलियन्स
- समारोह
- वस्तुओं
केवल अपनी वस्तुओं का प्रोटोटाइप बदलें।
प्रोटोटाइप संपत्ति
जावास्क्रिप्ट prototype
गुण आपको वस्तुओं में नए गुण जोड़ने की अनुमति देता है:
उदाहरण
function Person(first, last, age, eyecolor) {
this.firstName = first;
this.lastName = last;
this.eyeColor = eyecolor;
}
Person.prototype.nationality = "English";