简体   繁体   中英

access a function inside a javascript object

I'm having trouble accessing a function inside an object.

My setup is 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 setup, initialize doesn't run but if I take my google maps variables/functions out of the initialize function then it work's but my understanding (from google docs) is that initialize should always be a function that contains the google maps variables/functions etc.

Even if that is the right way to go it would be great to find out how to access functions when they are inside an objects method just for debug purposes in the console. I thought

CAREERS.init.initialize(); 

would work but it doesn't.

Any help or advice would be greatly appreciated.

Thanks

Giles

The initialize function is truly private to the function you're putting on this.init . It cannot be accessed from outside that this.init function unless you do something to make it accessible.

But I don't think you need that 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, though, your code is trying to initialize the Careers instance twice. You have Google's load callback calling jQuery's ready function which then calls your CareersInit function which calls CAREERS.init . But you also have the Careers constructure scheduling a separate page load callback. (That one may or may not run, it depends on when Google fires the setOnLoadCallback callback.)

I'd get rid of one of those calls to init .

In a comment on another answer, you've said you want to know what the "best" way to do it is. I'd have to know more about what you're doing, but I'd probably 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's init function
    Careers.prototype.init = Careers_init;
    function Careers_init()
    {
        //Usual google maps stuff here
    }

})();

...except that if you're going to just have one instance (and you're sure that's not going to change), there's really no call for a constructor function at all:

(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";
    }
})();

There, the function scope is the single instance; no need for separate constructor functions, playing games with this , etc. Note that we're not creating any globals, someData is scoped to the anonymous function. The call that the interpreter makes to that function is our single object.

If you're ever going to need more than one Career instance, then great, definitely go the constructor function route. But if not, there's a lot less fuss involved if you use the object you already have (the execution context of the call to the function).


Off-topic : Strongly recommend declaring your CAREERS variable. With your code as it is now, you're falling prey to The Horror Of Implicit Globals .

initialize is a private function inside init , which means it is inaccessible outside init .

What exactly is the purpose of defining things twice?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM