+ 3

Es6 Class declaration vs named vs unnamed class expression

I'm on my limit right now, I can't process information in my head. Can someone explain me the difference, pro and cons of these 3 class? In a simple word of course. I already searched them. It's just I'm having trouble in es6 tutorial here in SoloLearn (a lot of learners too). //------------Class Declaration------------ class Vihicle { constructor(height, width) { this.height = height; this.width = width; } }; //------------Named Class Expression------------- const Truck = class Vihicle { constructor(height, width) { this.height = height; this.width = width; } }; //------------Unamed Class Expression-------------- const Van = class { constructor(height, width) { this.height = height; this.width = width; } }

31st Aug 2022, 10:35 AM
Jonathan P. Jundarino
Jonathan P. Jundarino - avatar
1 Odpowiedź
0
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/class these three methods have the same end. There are very few differences. The first (class statements) is the way found in most OOP languages. We declare a class, then instances of this class with the New syntax. Once declared the class is no longer modifiable and the name of the class no longer reusable. The second (class expression) makes it possible to store a class in a variable and made (if it is not a const) the class can be modified later in the code. The third (Named class expression) allows to name the class in a class expression only to reuse the name of the class inside it
1st Sep 2022, 7:01 PM
Roland
Roland - avatar