demiculus Posted March 16, 2021 Share Posted March 16, 2021 Here is the full question https://stackoverflow.com/questions/66646134/checking-if-members-are-initiated-yet-in-constructor I have a class that extends `PIXI.Text` which overrides the default `this.text` with something like so: ``` set text(newText: string) { super.text = newText this.updateToolTipText(newText) } ``` The problem is that: 1. `set text` gets called during the construction of the class 2. and `this.udpateToolTipText()` uses `this.width` to adjust the container size BUT when called during the construction of the class, `this.width` isn't defined yet and throws this error: `TypeError: null is not an object (evaluating 'style.styleID')` I've tried `if(this.width == undefined) return` and `if(this.width == null) return` but nothing seems to prevent this error from triggering. After construction is complete, the class behaves as expected, this is only being thrown during construction. I've hacked around this solution by creating a dummy variable to return out during construction but I just don't understand how/why I can't check if `this.width` exists without throwing the error. Quote Link to comment Share on other sites More sharing options...
ivan.popelyshev Posted March 16, 2021 Share Posted March 16, 2021 (edited) set text(newText: string) { super.text = newText Are you sure super setters can even work in javascript/typescript? > `set text` gets called during the construction of the class if (!this.ready) { ... } in the end of overriden constructor: ready= true; as to WHY? question: its OOP, it has problems in all libs /frameworks. I really dont want to explain the sequence of how pixi initiates it because it can change later. If you want to alter this behaviour, just post issue or even PR in pixijs github. If you want to know WHY? - read the source code, because its the usual OOP initialization bullshit. Edited March 16, 2021 by ivan.popelyshev demiculus and Esqarrouth 2 Quote Link to comment Share on other sites More sharing options...
orwin kael Posted August 21 Share Posted August 21 When working with constructors, it helps to make sure every required member is initialized clearly and consistently before the object is used. This makes the code easier to understand and can prevent unexpected behavior caused by uninitialized values. The same basic idea applies to construction projects too, where checking existing walls before starting repair work can prevent bigger problems later. If someone is dealing with damaged masonry, searching for reliable brick wall repair san diego services can be a good starting point for assessing the condition and planning the necessary repairs. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.