Skip to content
Advertisement

How to call a method as soon as a class is initialized in JavaScript

I have a class to create a user, when I add a init() function to the class which should be executed as soon as the class is initialized, it returns a SyntaxError.

I followed this answer but get another SyntaxError

Can anyone tell me what I am doing wrong, I want dob and age to be set on initialisation

JavaScript

Advertisement

Answer

Alternatively to the other answer, you can call any initialising methods directly in the constructor – that is what the constructor is there to do.

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