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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T19:06:37+00:00 2026-05-29T19:06:37+00:00

Creating the closure is easy but using it is confusing for me. Here is

  • 0

Creating the closure is easy but using it is confusing for me. Here is my closure. Once I have it I need to be able to call operations on it like doWork, calculateThis, doAnimation, etc. but there doesn’t seem to be a way to access functions inside the closure.

function worker(input) {
    return function () {
        doWork = function () {
             alert("doing work");
        };
    }
}

function caller() {
    var myWorker = worker();
    myWorker.doWork(); // this fails
}

*The question you’re asking appears subjective and is likely to be closed. – Thanks again stackoverflow

  • 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-29T19:06:42+00:00Added an answer on May 29, 2026 at 7:06 pm

    I believe this is what you are asking for:

    function worker(input) {
        return {
            doWork: function () {
                 alert("doing work");
            },
            doAnimation: function() {
                 alert("animating");
            }
        }
    }
    

    You can now call it using your code:

    var myWorker = worker();
    myWorker.doWork();
    myWorker.doAnimation();
    

    Note that your code is not really using closures, but this one does:

    function worker(input) {
        return {
            doWork: function () {
                 alert("doing work: " + input);
            },
            doAnimation: function() {
                 alert("animating: " + input);
            }
        }
    }
    
    var workerA = worker('A');
    var workerB = worker('B');
    workerA.doWork();
    workerB.doAnimation();
    

    Can you see the difference?

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

Sidebar

Related Questions

I'm creating my own JavaScript Array-like object and I have methods that call closures.
I have a JavaScript object that does something like this - using a closure
I am using Javascript and am creating my function within a closure, the problem
Creating a patch is very easy in SubVersion, With Tortoise, you right-click and select
Creating a JApplet I have 2 Text Fields, a button and a Text Area.
I got an unexpected closure when creating a nested class. I suspect that this
I am trying to do async IO using BeginRead() in JScript.NET, but I can't
Should I feel wary about creating clojure keywords which have non-existent namespaces? An example
When using LINQ extension methods like Enumerable.Select, is it better to use a Lambda
I've seen these two approaches for creating js objects with closures, but wasn't sure

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.