Class member encapsulation
C++ has static inheritance and typing. this->A::x is 1 after B's
constructor delegates to A's constructor, and this->B::x (a different
field) is 2 once that field has been set in B::B.
JS has (not yet, anyway) no field declarations, no namespacing akin to A::x and B::x, and so only one this.x to mutate among the super and subclass when constructing a new instance of b.
JS ain't C++, no one has ever accused it of that (or me of "making it into C++", lol).
Le 4 janv. 2016 à 00:07, monolithed <monolithed at gmail.com> a écrit :
Why we have different behavior?!I know that ECMAScript classes are syntactical sugar, but this behavior is unexpected for many people...
The C++ behaviour indeed surprises me, but this mailing list is not exactly for discussing C++ class semantics. ;-) Also, "ECMAScript classes are syntactical sugar" is not an argument for discrepancy (and it is incorrect, anyway).
C++:
JavaScript:
class A { constructor () { this.x = 1; } call () { return this.x; } } class B extends A { constructor () { super(); this.x = 2; } } let b = new B(); b.call(); // 2
Why we have different behavior?!I know that ECMAScript classes are syntactical sugar, but this behavior is unexpected for many people...