Jump to content

How to check if members are initiated in constructor


demiculus
 Share

Recommended Posts

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. 
 

Link to comment
Share on other sites

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 by ivan.popelyshev
Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...