TLDR; অতি প্রয়োজনীয় নয়, তবে সম্ভবত এটি দীর্ঘমেয়াদে সহায়তা করবে এবং এটি করা আরও সঠিক।
দ্রষ্টব্য: আমার পূর্ববর্তী উত্তরটি বিভ্রান্তিকরভাবে লেখা হয়েছিল বলে অনেকগুলি সম্পাদনা করা হয়েছিল এবং এর কিছু ত্রুটি ছিল যা উত্তর দেওয়ার জন্য আমার ভিড়তে আমি মিস করি। যারা কিছু গুরুতর ত্রুটি চিহ্নিত করেছেন তাদের ধন্যবাদ।
মূলত, এটি জাভাস্ক্রিপ্টে সঠিকভাবে সাবক্লাসিংয়ের তারে রয়েছে। যখন আমরা সাবক্লাস করি তখন প্রোটোটাইপাল প্রতিনিধি কোনও prototypeবস্তুর ওভাররাইটিং সহ সঠিকভাবে কাজ করে তা নিশ্চিত করার জন্য আমাদের কয়েকটি মজার জিনিস করতে হবে । একটি prototypeবস্তুকে ওভাররাইটিংয়ে অন্তর্ভুক্ত রয়েছেconstructor , সুতরাং আমাদের তখন রেফারেন্সটি ঠিক করতে হবে।
ES5- এ 'ক্লাস' কীভাবে কাজ করে তা দ্রুত তাড়াতাড়ি চলুন।
ধরা যাক আপনার একটি কনস্ট্রাক্টর ফাংশন এবং এর প্রোটোটাইপ রয়েছে:
//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), যা আমরা কীভাবে মধ্যে যে delegational সম্পর্ক স্থাপন 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যেমন আমরা আশা। সম্ভবত আমরা এটি স্টুডেন্ট.প্রোটোটাইপ এবং পার্সন.প্রোটোটাইপ থেকে উত্তরাধিকার সূত্রে পেতে চাই। সুতরাং আমাদের এটি সব ঠিক করা প্রয়োজন।
/* 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
ইএস 5-তে, আমাদের constructorসম্পত্তিটি এমন একটি রেফারেন্স যা কোনও ফাংশনকে বোঝায় যা আমরা 'কনস্ট্রাক্টর' হওয়ার অভিপ্রায় নিয়ে লিখেছি। newকীওয়ার্ড আমাদের যা দেয় তা বাদ দিয়ে কনস্ট্রাক্টর অন্যথায় একটি 'প্লেইন' ফাংশন।
ES6- এ, 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;
}
}