TLDR; सुपर आवश्यक नहीं है, लेकिन संभवतः लंबे समय में मदद करेगा, और ऐसा करना अधिक सटीक है।
नोट: मेरे पिछले उत्तर के रूप में बहुत संपादित किया गया था भ्रामक रूप से लिखा गया था और इसमें कुछ त्रुटियां थीं, जिनका उत्तर देने के लिए मैं जल्दबाज़ी में चूक गया। उन लोगों के लिए धन्यवाद जिन्होंने कुछ अहंकारी त्रुटियों को इंगित किया।
मूल रूप से, यह जावास्क्रिप्ट में सबक्लासिंग को सही ढंग से तार करना है। जब हम उपवर्ग करते हैं, तो हमें यह सुनिश्चित करने के लिए कुछ फंकी चीजें करनी पड़ती हैं कि एक prototypeवस्तु को अधिलेखित करने में प्रोटोटाइप का प्रतिनिधिमंडल सही ढंग से काम करता है । किसी prototypeऑब्जेक्ट को ओवरराइट करने में ऑब्जेक्ट शामिल होता हैconstructor , इसलिए हमें संदर्भ को ठीक करने की आवश्यकता है।
आइए जल्दी से ईएस 5 काम में 'क्लास' कैसे करें।
मान लीजिए कि आपके पास एक निर्माण कार्य और इसका प्रोटोटाइप है:
//Constructor Function
var Person = function(name, age) {
this.name = name;
this.age = age;
}
//Prototype Object - shared between all instances of Person
Person.prototype = {
species: 'human',
}
जब आप कंस्ट्रक्टर को तुरंत फोन करते हैं, तो कहें Adam:
// instantiate using the 'new' keyword
var adam = new Person('Adam', 19);
newकीवर्ड 'व्यक्ति' के साथ आमंत्रण को मूल रूप से कोड की कुछ अतिरिक्त लाइनों के साथ व्यक्ति निर्माता चलेंगे:
function Person (name, age) {
// This additional line is automatically added by the keyword 'new'
// it sets up the relationship between the instance and the prototype object
// So that the instance will delegate to the Prototype object
this = Object.create(Person.prototype);
this.name = name;
this.age = age;
return this;
}
/* So 'adam' will be an object that looks like this:
* {
* name: 'Adam',
* age: 19
* }
*/
अगर हम console.log(adam.species), देखने में असफल हो जायेगी adamउदाहरण है, और इसके लिए मूलरूप श्रृंखला को देखने के .prototype, जो है Person.prototype- और Person.prototype है एक .speciesसंपत्ति है, तो देखने में सफल होगा Person.prototype। इसके बाद लॉग होगा'human' ।
यहाँ, Person.prototype.constructorसही ढंग से इंगित करेगा Person।
तो अब दिलचस्प हिस्सा है, तथाकथित 'उपवर्ग'। यदि हम एक Studentक्लास बनाना चाहते हैं , जो कि Personकुछ अतिरिक्त परिवर्तनों के साथ क्लास का एक उपवर्ग है , तो हमें यह सुनिश्चित करने की आवश्यकता होगी कि Student.prototype.constructorछात्र सटीकता के लिए अंक प्राप्त करें।
यह खुद से ऐसा नहीं करता है। जब आप उपवर्ग करते हैं, तो कोड इस तरह दिखता है:
var Student = function(name, age, school) {
// Calls the 'super' class, as every student is an instance of a Person
Person.call(this, name, age);
// This is what makes the Student instances different
this.school = school
}
var eve = new Student('Eve', 20, 'UCSF');
console.log(Student.prototype); // this will be an empty object: {}
new Student()यहां कॉलिंग उन सभी गुणों के साथ एक ऑब्जेक्ट लौटाएगी जिन्हें हम चाहते हैं। यहां, अगर हम जांच करते हैं eve instanceof Person, तो यह वापस आ जाएगी false। यदि हम एक्सेस करने की कोशिश करते हैं eve.species, तो यह वापस आ जाएगीundefined ।
दूसरे शब्दों में, हमें प्रतिनिधिमंडल को तार-तार करने की आवश्यकता है ताकि यह eve instanceof Personसही हो जाए और Studentप्रतिनिधि के उदाहरणों को सही ढंग से Student.prototypeऔर फिर Person.prototype।
लेकिन जब से हम इसे newकीवर्ड के साथ बुला रहे हैं , याद रखें कि क्या मंगलाचरण जोड़ता है? यह कॉल करेगा Object.create(Student.prototype), कि हम किस तरह से उस प्रतिनिधि संबंध को स्थापित करते हैं Studentऔर Student.prototype। ध्यान दें कि अभी Student.prototypeखाली है। इसलिए .speciesइसका उदाहरण देखने में Studentविफल हो जाएगा क्योंकि यह केवल प्रतिनिधि को सौंपता है Student.prototype, और .speciesसंपत्ति मौजूद नहीं है Student.prototype।
जब हम नियत कर सकता Student.prototypeकरने के लिए Object.create(Person.prototype), Student.prototypeअपने आप में तो प्रतिनिधियों Person.prototype, और को देख eve.speciesवापस आ जाएगी human, जैसा कि हम उम्मीद करते हैं। संभवत: हम चाहते हैं कि यह Student.prototype और Person.prototype से विरासत में मिले। तो हम सब को ठीक करने की जरूरत है।
/* This sets up the prototypal delegation correctly
*so that if a lookup fails on Student.prototype, it would delegate to Person's .prototype
*This also allows us to add more things to Student.prototype
*that Person.prototype may not have
*So now a failed lookup on an instance of Student
*will first look at Student.prototype,
*and failing that, go to Person.prototype (and failing /that/, where do we think it'll go?)
*/
Student.prototype = Object.create(Person.prototype);
अब प्रतिनिधिमंडल काम करता है, लेकिन हम Student.prototypeएक के साथ ओवरराइट कर रहे हैं Person.prototype। इसलिए यदि हम फोन करते हैं Student.prototype.constructor, तो Personइसके बजाय यह इंगित करेगा Student। यही कारण है कि हमें इसे ठीक करने की आवश्यकता है।
// Now we fix what the .constructor property is pointing to
Student.prototype.constructor = Student
// If we check instanceof here
console.log(eve instanceof Person) // true
ES5 में, हमारी constructorसंपत्ति एक संदर्भ है जो एक फ़ंक्शन को संदर्भित करती है जिसे हमने 'कंस्ट्रक्टर' होने के इरादे से लिखा है। newकीवर्ड हमें क्या देता है, इसके अलावा , कंस्ट्रक्टर अन्यथा 'प्लेन' फंक्शन है।
ईएस 6 में, constructorअब हम कक्षाओं को लिखने के तरीके में बनाया गया है - जैसे कि, यह एक विधि के रूप में प्रदान किया जाता है जब हम एक वर्ग घोषित करते हैं। यह बस वाक्य रचना चीनी है, लेकिन superजब हम एक मौजूदा वर्ग का विस्तार कर रहे हैं, तो यह हमें कुछ उपयुक्तताओं तक पहुंच प्रदान करता है । तो हम उपरोक्त कोड इस तरह से लिखेंगे:
class Person {
// constructor function here
constructor(name, age) {
this.name = name;
this.age = age;
}
// static getter instead of a static property
static get species() {
return 'human';
}
}
class Student extends Person {
constructor(name, age, school) {
// calling the superclass constructor
super(name, age);
this.school = school;
}
}