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);
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!
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.
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;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With