नया classसिंटैक्स अभी के लिए , ज्यादातर सिंटैक्टिक शुगर है। (लेकिन, आप जानते हैं, चीनी की अच्छी किस्म।) ES2015-ES2020 में ऐसा कुछ भी नहीं classहै जो आप कर सकते हैं कि आप निर्माण कार्यों और Reflect.construct(उपवर्ग Errorऔर including सहित Array) के साथ नहीं कर सकते । (यह है की संभावना देखते ES2021 में कुछ चीजें है कि आप के साथ कर सकते हो जाएगा classकि आप अन्यथा नहीं कर सकते: निजी क्षेत्रों , निजी तरीकों , और स्थिर क्षेत्रों / निजी स्थिर तरीकों ।)
इसके अलावा, classओओपी का एक अलग प्रकार है या यह अभी भी जावास्क्रिप्ट की प्रोटोटाइप विरासत है?
यदि आप कंस्ट्रक्टर फ़ंक्शंस ( new Fooआदि) का उपयोग करना पसंद करते हैं, तो यह वही प्रोटोटाइपिक विरासत है जो हमारे पास हमेशा होती है, बस क्लीनर और अधिक सुविधाजनक सिंटैक्स के साथ । (विशेष रूप से Arrayया Errorजिसे आप ES5 और पहले नहीं कर सकते थे, से प्राप्त करने के मामले में । अब आप Reflect.construct[ युक्ति , MDN ] के साथ कर सकते हैं , लेकिन पुराने ES5- शैली के साथ नहीं।)
क्या मैं इसका उपयोग करके संशोधित कर सकता हूं .prototype?
हाँ, आप prototypeक्लास बनाने के बाद भी क्लास के कंस्ट्रक्टर पर ऑब्जेक्ट को संशोधित कर सकते हैं । जैसे, यह पूरी तरह से कानूनी है:
class Foo {
constructor(name) {
this.name = name;
}
test1() {
console.log("test1: name = " + this.name);
}
}
Foo.prototype.test2 = function() {
console.log("test2: name = " + this.name);
};
गति लाभ हैं?
इसके लिए एक विशिष्ट मुहावरा प्रदान करके, मुझे लगता है कि यह संभव है कि इंजन एक बेहतर काम का अनुकूलन करने में सक्षम हो। लेकिन वे पहले से ही अनुकूलन में बहुत अच्छे हैं, मैं एक महत्वपूर्ण अंतर की उम्मीद नहीं करूंगा।
ES2015 (ES6) classसिंटैक्स क्या लाभ प्रदान करता है?
संक्षेप में: आप पहली जगह में निर्माता कार्यों का उपयोग नहीं करते हैं, पसंद करते हैं Object.createया इसी तरह, classआप के लिए उपयोगी नहीं है।
यदि आप निर्माण कार्यों का उपयोग करते हैं, तो इसके कुछ लाभ हैं class:
वाक्यविन्यास सरल और कम त्रुटि-प्रवण है।
यह बहुत आसान (और फिर, त्रुटि-रहित होता) वर्ष के साथ की तुलना में नए सिंटैक्स का उपयोग विरासत पदानुक्रम स्थापित करने के लिए।
classआपको newकंस्ट्रक्टर फ़ंक्शन के साथ उपयोग करने में विफल होने की सामान्य त्रुटि से बचाव करता है (यदि कंस्ट्रक्टर thisएक वैध ऑब्जेक्ट नहीं है, तो कंस्ट्रक्टर ने एक अपवाद फेंक दिया है)।
किसी विधि के मूल प्रोटोटाइप के संस्करण को कॉल करना पुराने ( या के super.method()बजाय ) की तुलना में नए सिंटैक्स के साथ बहुत सरल है ।ParentConstructor.prototype.method.call(this)Object.getPrototypeOf(Object.getPrototypeOf(this)).method.call(this)
यहाँ एक पदानुक्रम के लिए एक वाक्यविन्यास तुलना है:
class Person {
constructor(first, last) {
this.first = first;
this.last = last;
}
personMethod() {
}
}
class Employee extends Person {
constructor(first, last, position) {
super(first, last);
this.position = position;
}
employeeMethod() {
}
}
class Manager extends Employee {
constructor(first, last, position, department) {
super(first, last, position);
this.department = department;
}
personMethod() {
const result = super.personMethod();
return result;
}
managerMethod() {
}
}
उदाहरण:
class Person {
constructor(first, last) {
this.first = first;
this.last = last;
}
personMethod() {
return `Result from personMethod: this.first = ${this.first}, this.last = ${this.last}`;
}
}
class Employee extends Person {
constructor(first, last, position) {
super(first, last);
this.position = position;
}
personMethod() {
const result = super.personMethod();
return result + `, this.position = ${this.position}`;
}
employeeMethod() {
}
}
class Manager extends Employee {
constructor(first, last, position, department) {
super(first, last, position);
this.department = department;
}
personMethod() {
const result = super.personMethod();
return result + `, this.department = ${this.department}`;
}
managerMethod() {
}
}
const m = new Manager("Joe", "Bloggs", "Special Projects Manager", "Covert Ops");
console.log(m.personMethod());
बनाम
var Person = function(first, last) {
if (!(this instanceof Person)) {
throw new Error("Person is a constructor function, use new with it");
}
this.first = first;
this.last = last;
};
Person.prototype.personMethod = function() {
};
var Employee = function(first, last, position) {
if (!(this instanceof Employee)) {
throw new Error("Employee is a constructor function, use new with it");
}
Person.call(this, first, last);
this.position = position;
};
Employee.prototype = Object.create(Person.prototype);
Employee.prototype.constructor = Employee;
Employee.prototype.employeeMethod = function() {
};
var Manager = function(first, last, position, department) {
if (!(this instanceof Manager)) {
throw new Error("Manager is a constructor function, use new with it");
}
Employee.call(this, first, last, position);
this.department = department;
};
Manager.prototype = Object.create(Employee.prototype);
Manager.prototype.constructor = Manager;
Manager.prototype.personMethod = function() {
var result = Employee.prototype.personMethod.call(this);
return result;
};
Manager.prototype.managerMethod = function() {
};
लाइव उदाहरण:
var Person = function(first, last) {
if (!(this instanceof Person)) {
throw new Error("Person is a constructor function, use new with it");
}
this.first = first;
this.last = last;
};
Person.prototype.personMethod = function() {
return "Result from personMethod: this.first = " + this.first + ", this.last = " + this.last;
};
var Employee = function(first, last, position) {
if (!(this instanceof Employee)) {
throw new Error("Employee is a constructor function, use new with it");
}
Person.call(this, first, last);
this.position = position;
};
Employee.prototype = Object.create(Person.prototype);
Employee.prototype.constructor = Employee;
Employee.prototype.personMethod = function() {
var result = Person.prototype.personMethod.call(this);
return result + ", this.position = " + this.position;
};
Employee.prototype.employeeMethod = function() {
};
var Manager = function(first, last, position, department) {
if (!(this instanceof Manager)) {
throw new Error("Manager is a constructor function, use new with it");
}
Employee.call(this, first, last, position);
this.department = department;
};
Manager.prototype = Object.create(Employee.prototype);
Manager.prototype.constructor = Manager;
Manager.prototype.personMethod = function() {
var result = Employee.prototype.personMethod.call(this);
return result + ", this.department = " + this.department;
};
Manager.prototype.managerMethod = function() {
};
var m = new Manager("Joe", "Bloggs", "Special Projects Manager", "Covert Ops");
console.log(m.personMethod());
जैसा कि आप देख सकते हैं, बहुत सारे दोहराए गए और क्रियाओं के सामान जो कि गलत होना आसान है और फिर से लिखना उबाऊ है (यही कारण है कि मैंने इसे करने के लिए एक स्क्रिप्ट लिखी थी, दिन में वापस)।
¹ "ES2015-ES2018 में कुछ भी नहीं है कि नहीं है classकि आप निर्माता कार्यों के साथ ऐसा नहीं कर सकते कर सकते हैं और कर सकते हैं Reflect.construct(सहित उपवर्गीकरण Errorऔर Array)"
उदाहरण:
function MyError(...args) {
return Reflect.construct(Error, args, this.constructor);
}
MyError.prototype = Object.create(Error.prototype);
MyError.prototype.constructor = MyError;
MyError.prototype.myMethod = function() {
console.log(this.message);
};
function outer() {
function inner() {
const e = new MyError("foo");
console.log("Callng e.myMethod():");
e.myMethod();
console.log(`e instanceof MyError? ${e instanceof MyError}`);
console.log(`e instanceof Error? ${e instanceof Error}`);
throw e;
}
inner();
}
outer();
.as-console-wrapper {
max-height: 100% !important;
}