JavaScript new this.constructor (); => SyntaxError: missing] after the list of elements

I have several classes:

class Sample{
    static createSelf() {
        return new this.constructor(1, 2);
    }
}

class AnotherClass extends Sample {
    constructor(a, b) {
        this.c = a+b;
    }
}

ac = AnotherClass.createSelf();

How should I do it?

This specific example gives me SyntaxError: missing formal parameter, although in my source code (500 lines), when I have it new this.constructor(), I get SyntaxError: missing ] after element listthe first line (the formal parameter error is indicated on line 1, too). I know this is because of this line, because when I replace it with the usual class name, it works. Array initialization is closed. An error cannot mean:

There is an error in the syntax of the array initializer. There is probably a closing bracket ("]") or a comma (",") is missing.

from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Missing_bracket_after_list

UPDATE Source Code:

class Participant {
    constructor(origin, destination, number, startDate, endDate) {
        ...
    }

    static restore(save) {
        const participant = new this.constructor(
            new Marker(save.originLocation, this.getMarkerOptions(true, save.isDriver, save.number)).addTo(map),
            new Marker(save.destinationLocation, this.getMarkerOptions(false, save.isDriver, save.number)).addTo(map),
            save.number,
            save.startDate,
            save.endDate
        );

        return participant;
    };
}

class Driver extends Participant {}

d = Driver.restore(saveObject);
+4
1

, , . >

, , this.constructor Function, . JavaScript , .

( ):

new Function({});
Hide result

, , .

this

this , . -, this -. AnotherClass.createSelf();, this AnotherClass. this.constructor Function. , . ,

class Sample{
    static createSelf() {
        return new this(1, 2);
    }
}

, , this , ? .

this

, super() , this:

class AnotherClass extends Sample {
    constructor(a, b) {
        super();
        this.c = a+b;
    }
}
+4

Source: https://habr.com/ru/post/1685766/


All Articles