If I have a javascript class that runs some initialization code, it seems logical to put this code at the top and any methods at the bottom of the class. The problem is that if the initialization code contains a method call, I get "undefined", this is not a function error. I assume that the method is defined after the method call. How do people usually structure javascript classes to avoid this? Did they put all the initialization code at the end of the class? For instance:
var class = function() { this.start(); this.start = function() { alert('foo'); }; }; var object = new class();
causes an error, but:
var class = function() { this.start = function() { alert('foo'); }; this.start(); }; var object = new class();
not. what would be considered a good structure for a javascript object like this?
source share