Where is the β€œright” place to initialize class variables in AS3

Is it "better" to initialize class variables in AS3 class constructor? Or can I just initialize them by default when I declare them at the top of my class? I ask, because when there are many class variables, it seems inefficient to declare them in one place, and then initialize them in another, when I could easily do it in one place. This one is better than the other, why?

Thanks!

eg:

constructor initialization

class foo { var bar:Boolean } function foo():void { bar = true; } 

OR

initialization by declaration

 class foo { var bar:Boolean = true; } function foo():void { } 
+2
source share
2 answers

I personally recommend initialization inside the constructor for two reasons:

  • You cannot initialize objects that depend on other var objects already created before construction.
  • It is easier to find and understand the initialization code when it is correctly procedural. Especially when you (I) immerse yourself in large classes, I did not open for several months.
+2
source

Personally, I do not do this either. I like to create an init function that initializes everything. This means that if you want to reset the default instance, I can just call the init method at any time.

+1
source

Source: https://habr.com/ru/post/885834/


All Articles