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

  • Home
  • SEARCH
  • 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 7185109
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:20:34+00:00 2026-05-28T18:20:34+00:00

I have the following (simplified) code: var Foo = (function () { var data

  • 0

I have the following (simplified) code:

var Foo = (function () {
    var data = {},
        settings = {
            // default settings here
        };

    function bar(callback) { // bar is an asynchronous function
        var result = null;
        // fiddle around until you get a result
        if (callback) {
            callback(result);
        }
    }

    return {
        init: function (options, callback) {
            var kallback = callback;
            $.extend(settings, options);
            bar(function () {
                if (kallback) {
                    kallback(WHAT_GOES_HERE);
                }
            });
        },
        debug: function () {
            return {
                settings: settings,
                data: data
            };
        },
        set: function (k, v) {
            settings[k] = v;
        },
        get: function (k) {
            return settings[k];
        }
    };
}());

The code above is in a js file, then in the footer of the page in question:

<script type="text/javascript">
    Foo.init({ option1: "value", option2: "value" }, function (obj) {
        console.log("The object was ", obj);
    });
</script>

Basically, here is what I want to be able to do:

  1. Create an object (with a set of optional params, but not important for this question)
  2. During the creation phase of the object, have it call an asynchronous function
  3. When the asynchronous function is done, I should be able to trigger a callback, and the argument for the callback should be the intialized object

I thought that this would work for WHAT_GOES_HERE above, but turns out, at least when I’ve tested it, that this is the DOM Window object.

First of all, am I constructing this object correctly? Or is there a better way to create it?

Secondly, assuming I am doing this right, what should go in the WHAT_GOES_HERE so that when console.log("The object was ", foo); runs, the value for obj is the created Foo object?

  • 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-28T18:20:35+00:00Added an answer on May 28, 2026 at 6:20 pm

    Yes, in an anonymous function called this way, this will refer to the window object. To refer to the this reference from the init method you have to store the reference in another variable:

    var kallback = callback, self = this;
    $.extend(settings, options);
    bar(function () {
        if (kallback) {
            kallback(self);
        }
    });
    

    Your way of constructing the object works, as long as you only want to have one single foo object.

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

Sidebar

Related Questions

I have a following (simplified) code: var myModule = { submitDummyForm: function(){ console.log(this); //
I have the following (here simplified) code which I want to test with FakeItEasy
Imagine I have the following code (simplified regarding my real context of course): <div
I'm using gcc 4.3.2. I have the following code (simplified): #include <cstdlib> template<int SIZE>
I am debugging some code and have encountered the following SQL query (simplified version):
I have a MySQL table with the following data (simplified): INSERT INTO `stores` (`storeId`,
I have the following code (asp.net-mvc, jquery) (i have simplified the example to show
I have an aspx page with the following code (partial and simplified): (beginning of
I have the following code(simplified). public class OrderProcessor { public virtual string PlaceOrder(string test)
I have the following code (simplified for the sake of discussion). What I don't

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.