[SOLVED] What is wrong with this class definition?

I want to use classes for my game logic. But when creating the first one I got an error. I think I do everything according the standard.

This is the code:

class Biquad
{

    mA1=0;
    mA2=0;
    mB0=1.0;
    mB1=0;
    mB2=0;
    mX1=0;
    mX2=0;
    mY1=0;
    mY2=0;

    constructor() {
    }

	init(a1, a2, b0, b1, b2) {
         // see https://arachnoid.com/BiQuadDesigner/index.html for calculator
        this.mA1 = a1;
        this.mA2 = a2;
        this.mB0 = b0;
        this.mB1 = b1;
        this.mB2 = b2;
        this.mX1 = 0;
        this.mX2 = 0;
        this.mY1 = 0;
        this.mY2 = 0;
    }

	initVal(val) {
        this.mX1 = val;
        this.mX2 = val;
        this.mY1 = val;
        this.mY2 = val;
    }

	initAllPass() {
        this.mA1 = 0;
        this.mA2 = 0;
        this.mB0 = 1.0;
        this.mB1 = 0;
        this.mB2 = 0;
        this.mX1 = 0;
        this.mX2 = 0;
        this.mY1 = 0;
        this.mY2 = 0;
    }

	initLowPass(fs, fc, Q) {
        this.updateLowPass(fs, fc, Q);
        this.mX1 = 0;
        this.mX2 = 0;
        this.mY1 = 0;
        this.mY2 = 0;
    }

	updateLowPass(fs, fc, Q) {
        const w0 = 2.0 * Math.PI * fc / fs;
        const a = sin(w0) / (2.0 * Q);
        const a0 = 1.0 + a;
        this.mA1 = (-2.0 * cos(w0)) / a0;
        this.mA2 = (1.0 - a) / a0;
        this.mB1 = (1.0 - cos(w0)) / a0;
        this.mB0 = this.mB1 / 2.0;
        this.mB2 = this.mB0;
    }

	initHighPass(fs, fc, Q) {
        this.updateHighPass(fs, fc, Q);
        this.mX1 = 0;
        this.mX2 = 0;
        this.mY1 = 0;
        this.mY2 = 0;
    }

	updateHighPass(fs, fc, Q) {
        const w0 = 2.0 * Math.PI * fc / fs;
        const a = sin(w0) / (2.0 * Q);
        const a0 = 1.0 + a;
        this.mA1 = (-2.0 * cos(w0)) / a0;
        this.mA2 = (1.0 - a) / a0;
        this.mB1 = -(1.0 + cos(w0)) / a0;
        this.mB0 = -this.mB1 / 2.0;
        this.mB2 = this.mB0;
    }

	filter(x) {
        const y = this.mB0 * x + this.mB1 * this.mX1 + this.mB2 * this.mX2 - this.mA1 * this.mY1 - this.mA2 * this.mY2;
        this.mX2 = this.mX1;
        this.mX1 = x;
        this.mY2 = this.mY1;
        this.mY1 = y;
        return y;
    }
}

This is the error:

ES6 classes don’t allow properties to be defined outside the functions. You will need to add these to the constructor

    constructor() {
        this.mA1 = 0;
    }

I’m not sure that’s the problem @yaustar - members outside constructor as just fine, see how we use those in the engine:

AFAIK, that is only supported in ES7 and above.

1 Like

It did not help:

It helps when I put all those lines inside the constructor. But that is not how the standard is defined. Isn’t it?
What level does PlayCanvas support? ES6 ( ECMAScript 2015)?

Which is what I suggested :slight_smile:

It’s not so much PlayCanvas. The way the scripts are added to the page is down in a ‘as-is’ way. It quite literally gets the JS file and adds it to the document. So anything that can run directly in the browser without imports/exports will work.

The linter we are using I believe is up to ES6.

Which standard?

Well it is a book about javascript and it has a chapter on classes. But probably it needs a higher version then ES6.

AFAIK, field declarations wasn’t part of the standard until after ES6. GitHub - tc39/proposal-class-fields: Orthogonally-informed combination of public and private fields proposals

Don’t quote me on that as I’m not 100% sure but regardless, it looks like the Editor linter doesn’t support it.