Skip to content
Advertisement

Properties are undefined within a decorator

I am trying to call a function that that has a decorator attached to it. What it does is run every x seconds which works just fine, however, when I try to log the properties in the function they output undefined, but in the constructor it displays the proper class.

If I call another function that is in the class, the function gets called, the issue just seems to be when accessing properties.

Here are a few of the ways I have tried to call the method:

JavaScript

Here is how I am implementing the decorator:

JavaScript

Here is a stackblitz example

Edit

After playing for a bit, I can see that I can do this:

JavaScript

Then when I call it in the class the repeater works:

JavaScript

However, this is not what I want to resort to, I would like the function to be called automatically and not manually.

Advertisement

Answer

So, to solve my issue I had to use a decorator at the class level along with at the method level. Then, in the constructor I run the method on the parent class.

First import the reflect-metadata module:

JavaScript
JavaScript

Here is the repeat function:

JavaScript

Here is how it is implemented in the class:

JavaScript

Here is the stackblitz example.

Advertisement