Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ES6 constructor inheritance

I was looking into ES6, because it appears to be promoted when writing reactJS apps. I was a bit surprised about the way constructors work:

class Human{ 
    constructor(name){ 
        this.name = name;
    }
    toString(){ 
        return "Name = " + this.name; 
    } 
}

class Person extends Human{} 

var person = new Person("kim"); 

How likely is it that one of the following might make my app brittle when writing a front end app in JS using class based system:

  • Parametered constructors are implicitly inherited (is this a good idea overall and what is the chance that it will stay like that?);
  • Disability to overload constructors within the same class;
  • Disability to overload based on parameter types;

Optional question:
The secondary question I have is on how smart of a decision it would be to use ES6 for large production apps today and if it's useful to just use ES6 for experimenting for now. I'm not sure whether it's misplaced to think that JS is tending towards a full fledged class based system like Java, and that things might change (evolute) faster than my code base will be able to digest.

like image 851
html_programmer Avatar asked Aug 23 '15 17:08

html_programmer


1 Answers

Parametered constructors are implicitly inherited - is this a good idea overall and what is the chance that it will stay like that?

Yes, it will stay like that. If you are not providing a constructor, the default is to call the parent constructor with all the parameters.

This is a good idea, as class does oblige you to call super() within the constructor (which is necessary at least for inheriting builins, and a good practise anyway). If there was no default constructor, you'd have to explicitly provide it yourself, which would have led to an increase in boilerplate code (and the goal of the class keyword was to avoid that).

Disability to overload constructors within the same class; Disability to overload based on parameter types

No. Of course, in JS you cannot have multiple constructors for a class, but you can still overload it, both on the number of passed arguments and their types.

Is ES6 production-ready?

Yes. The standard is adopted, there is no more experimentation with it. Transpilers do support all of the main useful syntax features.

I'm not sure whether it's misplaced to think that JS is tending towards a full fledged class based system like Java

Yes, that's misplaced. JS does not have a Java-like class system and will hardly ever get one. See also Will JavaScript ever become a 'proper' class based language?.

like image 53
Bergi Avatar answered Oct 18 '22 00:10

Bergi