Class properties being reset to undefined

Why is this happening? I have stepped through the debugger and no matter what, if I set a property in super() it gets reset to undefined
1 Reply
Deno AI Helper
Deno AI Helper4mo ago
I wasn't able to find the answer to your question in the official Deno documentation. However, based on the information you've provided, it's difficult to diagnose the exact issue without seeing the code. In JavaScript and TypeScript (which Deno uses), if you're setting a property in the super() constructor call, it should be available to the subclass unless it's being overwritten somewhere else in the subclass. Here's a generic example of how class inheritance should work:
class Base {
constructor(public prop: any) {}
}

class Derived extends Base {
constructor() {
super("initial value");
console.log(this.prop); // Should log "initial value"
}
}

const instance = new Derived();
class Base {
constructor(public prop: any) {}
}

class Derived extends Base {
constructor() {
super("initial value");
console.log(this.prop); // Should log "initial value"
}
}

const instance = new Derived();
In the above example, prop should not be undefined after the super() call unless it's being explicitly set to undefined later in the Derived class constructor or in some method that gets called before you check the value. Please note that this advice is not based on the Deno documentation, and I would advise you to double-check the accuracy of the advice given. If you continue to experience issues, you might want to look for any code that could be modifying the property after the super() call or consider sharing a minimal code example that reproduces the issue for more targeted help.