JavaScript Introducing ES2015 Classes Getter and Setter Methods

jlampstack
jlampstack
17,749 Points

Constructor placement / location?

In every video Guil has the constructor near the bottom of the class with the methods before. For me it seems more natural to have the constructor first, at the top. The code seems to work both ways.

Is the way Guil defines a constructor the preferred convention? Or does it not matter?

2 Answers

Steven Parker
Steven Parker
171,904 Points

Technically, it doesn't matter.

And i'm not sure if there's an actual "convention"; but like you, I also prefer to place constructors at the top.

lauralouise
lauralouise
14,845 Points

Just a note about this -- it technically doesn't matter where the constructor goes, but I've noticed that Workspaces does not like it if the constructor goes at the very top. Not sure why this is. If you put your constructor at the top, with no functions above it, the syntax highlighting (i.e. the different colors) goes all wonky.