ffxhand ffxhand - 25 days ago 6
Javascript Question

Class Refuses to Inherit Values

EDIT: I've made some progress in locating the issue. When calling

super()
in the Configuration class, it's not taking on any values no matter what I feed into it.

I'm trying to load the Configuration class, however when I try to log its values it simply comes up as
Configuration {}
, even when
require("util").inspect()
ed.

in config.js, class Configuration

class Configuration extends Object {
constructor(path, app) {
super(_.config);
_.path = path;
try {
_.config = reload(path);
console.log(_.config);
} catch(err) {
this.setup(() => {
_.config = reload(path);
}, (err) => app.logger.critical);
}
}

...

}


Here's how I'm instantiating it:

in application.js, class Application

this.config = new Config(this.root + "/config.json", this);
logger.debug(`Config instantiated`, `values = ${require("util").inspect(this.config)}`); // prints "DEBUG • Config instantiated • values = Configuration {}"

Answer Source

I ended up figuring out a solution to this issue. Whilst Jeff M, in the comments, suggested this could have been a V8 bug, it is actually acting according to the ECMAScript spec.

The main change is that I am no longer extending Object.

Here's the concept I've landed on: (code is heavily stripped down and simplified)

let inherit = { foo: "bar", baz: 4 };

class TestClass {
  constructor() {
    this.test = "thing";
    // this = TestClass {
    //          test: "thing" }

    Object.assign(this, inherit);
    // this = TestClass { 
    //          test: "thing",
    //          foo: "bar",
    //          baz: 4 }
  }
}