最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

ecmascript 6 - JavaScript new this.constructor(); => SyntaxError: missing ] after element list - Stack Overflow

programmeradmin2浏览0评论

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 ma (",") missing.

from

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);

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 ma (",") missing.

from https://developer.mozilla/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);
Share Improve this question edited Sep 15, 2017 at 18:24 Adam asked Sep 15, 2017 at 17:42 AdamAdam 1,8175 gold badges23 silver badges35 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 6

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;
    }
}
发布评论

评论列表(0)

  1. 暂无评论