Skip to content
Advertisement

How to call getter and setter from within a javascript class

Is it possible to call getter and setter from within own class ? If yes what’s the valid syntax instead of invalid

JavaScript

in below class :

JavaScript

Advertisement

Answer

Setters are called during asignment and getters during reading.

JavaScript

will call the setter passing “testing…” as a value.

JavaScript

will call the getter

If you need to treat as a callable method, wrap it inside a method or don’t use properties (getters/setters), use a method instead. If, as you show, you want to wrap it in another method for some reason:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement