Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6141821
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:18:07+00:00 2026-05-23T18:18:07+00:00

I’m having trouble accessing a function inside an object. My setup is as such..

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T18:18:08+00:00Added an answer on May 23, 2026 at 6:18 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
i got an object with contents of html markup in it, for example: string
I need a function that will clean a strings' special characters. I do NOT
I want to construct a data frame in an Rcpp function, but when I
I'm making a simple page using Google Maps API 3. My first. One marker
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.