Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

I have some 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 do I do this?

This concrete example gives me SyntaxError: missing formal parameter, although in my original code (500 lines), when I have new this.constructor(), I get SyntaxError: missing ] after element list pointed at the first line (the formal parameter error is pointed at line 1, too). I know it is because of this line, as when I replace it with a normal class name, it works. There is no Array initialization close. The error cannot possibly mean:

There is an error with the array initializer syntax somewhere. Likely there is a closing bracket ("]") or a comma (",") missing.

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

UPDATE original 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);
like image 941
Adam Avatar asked Sep 15 '17 17:09

Adam


People also ask

How do I fix SyntaxError missing after argument list?

The "SyntaxError: missing ) after argument list" occurs when we make a syntax error when calling a function, e.g. forget to separate its arguments with a comma. To solve the error make sure to correct any syntax errors in the arguments list of the function invocation. Copied!

What is missing after argument list?

The JavaScript exception "missing ) after argument list" occurs when there is an error with how a function is called. This might be a typo, a missing operator, or an unescaped string.


1 Answers

If the error is pointing to the first line, then the syntax error is before the code you posted here.

Turns out the reason for this error is that this.constructor refers to Function which will evaluate one of the arguments passed to it as code. Since you are not passing JavaScript code to the it, you are getting a syntax error.

Example (open your browser's console):

new Function({});

However, there are two problems with the code you posted here as well.

this inside static methods

The value of this depends on how a function is called. Static methods are called as methods of the constructor function, therefore this refers to the constructor function. In your example, with AnotherClass.createSelf();, this refers to AnotherClass. Therefore this.constructor refers to Function. I don't think that's what you want. I guess you want

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

It looks like you thought this would refer to an instance of the class, but how could it? You haven't created one yet.

this inside constructors

When a class extends another class, you always have to call super() inside the constructor before you are accessing this:

class AnotherClass extends Sample {
    constructor(a, b) {
        super();
        this.c = a+b;
    }
}
like image 185
Felix Kling Avatar answered Oct 11 '22 14:10

Felix Kling