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 6044581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:01:41+00:00 2026-05-23T07:01:41+00:00

I have a Factory class that creates a Widget object. The Factory object needs

  • 0

I have a Factory class that creates a Widget object. The Factory object needs to callback a “private method” of the Widget object at a later time to pass it some ajax info. So far, the only implementation I’ve come up with is to create a public method in the Widget that returns the private method to the factory, and then deletes itself, the Factory then returns the new Widget while retaining a pointer to the private method. Here is a simplified example:

function Factory()
{
    var widgetCallback = null;

    this.ajaxOperation = function()
    {
        //some ajax calls
        widgetCallback('ajaxresults');
    }

    this.getNewWidget = function()
    {
        var wid = new Widget();
        widgetCallback = wid.getCallback();
        return wid;
    }

    function Widget()
    {
        var state = 'no state';
        var self = this;
        var modifyState = function(newState)
        {
            state = newState;
        }

        this.getState = function()
        {
            return state;
        }

        this.getCallback = function()
        {
            delete self.getCallback;
            return modifyState;
        }
    }
}

Is there a better way to achieve the effect I’m after or is this a fairly reasonable approach? I know it works, just curious if I’m stepping into any pitfalls I should be aware of.

  • 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-23T07:01:41+00:00Added an answer on May 23, 2026 at 7:01 am
    this.getNewWidget = function() {
        var val = new Widget(),
            wid = val[0],
            widgetCallback = val[1];
    
        return wid;
    }
    
    function Widget() {
        var state = 'no state';
        var self = this;
        var modifyState = function(newState) {
            state = newState;
        }
    
        this.getState = function() {
            return state;
        }
    
        // Return tuple of Widget and callback
        return [this, modifyState];
    }
    

    Just get your constructor to return a Tuple<Widget, function>

    Alternative just use closure scope to edit widgetCallback directly in your Widget constructor

    function Factory() {
        var widgetCallback = null;
    
        this.ajaxOperation = function() {
            //some ajax calls
            widgetCallback('ajaxresults');
        }
    
        this.getNewWidget = function() {
            return new Widget();;
        }
    
        function Widget() {
            var state = 'no state';
            var self = this;
            // set it directly here!
            widgetCallback = function(newState) {
                state = newState;
            }
    
            this.getState = function() {
                return state;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a factory-type class that creates its products based on the traits of
I have a factory class that populates objects with data. I want to implementing
I have a factory that returns an interface FormatService : public class FormatServiceFactory {
I have learnt the factory method design pattern, and at the same time, I
I have some .NET remoting code where a factory method, implemented in some server
I have the following code for factory design pattern implementation. class Pen{ public: virtual
I have a factory that builds the objects with longest lifetime in my application.
I have this factory method in java: public static Properties getConfigFactory() throws ClassNotFoundException, IOException
I have a object built through a factory containing my parameters read from the
No, this is not a question about generics. I have a Factory pattern with

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.