开发者

access a function inside a javascript object

开发者 https://www.devze.com 2023-03-21 14:16 出处:网络
I\'m having trouble accessing a function inside an object. My setup is as such.. google.setOnLoadCallback(function(){$(document).ready(CareersInit);});

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
 }
0

精彩评论

暂无评论...
验证码 换一张
取 消