The initialize function is indeed private to the function that you put on this.init . Access to it from outside this.init not available unless you do something to make it accessible.
But I donβt think you need an extra layer of indirection:
google.setOnLoadCallback(function(){$(document).ready(CareersInit);}); function CareersInit() { CAREERS = new Careers(); CAREERS.init(); } function Careers() { var self = this; this.init = function() {
Separately, however, your code attempts to initialize a Careers instance twice. You have a Google download callback calling jQuery ready , which then calls your CareersInit function, which calls CAREERS.init . But you also have a Careers framework planning a separate page load callback. (Whether it can be started or not depends on when Google launches the setOnLoadCallback .)
I would get rid of one of these init calls.
In the comment on the other answer, you said you want to know what the βbestβ way to do this is. I would have to learn more about what you are doing, but I would do it like this:
(function() { // Our single Careers instance var CAREERS; // Ask Google to call us when ready google.setOnLoadCallback(function(){ // Just in case Google is ready before the DOM is, // call our init via `ready` (this may just call // us immediately). $(document).ready(CareersInit); }); // Initialize our single instance function CareersInit() { CAREERS = new Careers(); CAREERS.init(); } // Constructor function Careers() { } // Career init function Careers.prototype.init = Careers_init; function Careers_init() { //Usual google maps stuff here } })();
... except that if you only have one instance (and you are sure that you are not going to change), there is actually no call to the constructor function:
(function() {
Here, the scope is the only instance; no need for separate design functions, play games with this , etc. Please note that we do not create any global variables, someData to an anonymous function. The call that the interpreter makes for this function is our only object.
If you ever need more than one instance of Career , then great, definitely go along the path of the constructor function. But if not, the less problems arise if you use an existing object (the context of the function call execution).
Off topic : Strongly recommend declaring your Careers variable. With your code, as of right now, you fall prey to the horror of implicit globals .