Function access inside javascript object

I am having trouble accessing a function inside an object.

My setup as such.

google.setOnLoadCallback(function(){$(document).ready(CareersInit);}); function CareersInit() { CAREERS = new Careers(); CAREERS.init(); } function Careers() { this.init = function() { function initialize() { //Usual google maps stuff here } } $('body').bind('onload', function() { initialize(); }); } 

With this setting, initialization does not start, but if I use the google maps variables / functions from the initialization function, then it works, but my understanding (from google docs) is that initialize should always be a function that contains google displays variables / functions etc.

Even if this is the right way, it would be great to know how to access functions when they are inside the object method, just for debugging purposes in the console. I thought that

 CAREERS.init.initialize(); 

will work, but it is not.

Any help or advice is appreciated.

thanks

Gilles

+4
source share
2 answers

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() { //Usual google maps stuff here }; $('body').bind('onload', function() { self.init(); }); } 

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() { // Our data; the function *is* the single object var someData; // 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() { someData = "some value"; } })(); 

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 .

+3
source

initialize is a private function inside init , which means that it is not available outside init .

What is the purpose of identifying things twice?

  this.init = function() { //Usual google maps stuff here } 
0
source

All Articles