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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:36:35+00:00 2026-06-14T09:36:35+00:00

I am given a function like this: //! Work on record. /*! \param[in] Record

  • 0

I am given a function like this:

//! Work on record.
/*!
    \param[in]  Record                  (object) Record.
    \param[in]  AsyncCallback           (function) Asynchronous callback which is called when the operation is complete. It takes no parameters.
*/
function WorkOnRecord(Record, AsyncCallback)
{
    /* Work on Record asynchronously (e.g. by calling $.ajax())
       and call AsyncCallback on complete. */
}

Now, I have an array of objects (RecordArray) that I need to feed to the above function, one by one, meaning that I have to wait until it callbacks before calling it the next time.

So I came up with:

$(function() {
    var RecordArray = SomeOtherFunc();      // RecordArray is an array of objects.

    // Work on records, one by one.
    var RecordIndex = 0;
    var WorkFunc = function() {
        // If there are still records to work on...
        if(RecordIndex < RecordArray.length)
        {
            // Work on record.
            WorkOnRecord(RecordArray[RecordIndex], function() {
                // Move forward to next record.
                WorkFunc();
            });
            RecordIndex++;
        }
        // If there are no more records to work on...
        else
        {
            /* We are all done. */
        }
    };
    WorkFunc(); // Start working.
});

As you can see, WorkFunc is actually called from within the anonymous function on which the variable WorkFunc itself is defined. Is this legal in ECMAScript/Javascript? (legal meaning that it works on all standard-compliant browsers)

I mean, to me, it is pretty much like var c = c + 1; in Javascript or int c = c + 1; in C/C++/ObjC/Java, which is referencing a variable while defining it, so either it should be illegal or it’s behaviour should be undefined.

However, it seems to work fine on a few browsers.


After some more research and thinking, I came up with other solutions.

  • Solution 1: Naming the anonymous function (MyFunc), so that I can use its name inside (reference here).

Code:

var WorkFunc = function MyFunc() {  // Name it MyFunc.
    if(RecordIndex < RecordArray.length)
    {
        WorkOnRecord(RecordArray[RecordIndex], function() {
            MyFunc();   // Use MyFunc here
        });
        RecordIndex++;
    }
};
WorkFunc();
  • Solution 2: Use a function declaration instead of a function expression, so that it is more like a recursive function (although not exactly) (reference here).

Code:

function WorkFunc() {   // Declare function.
    if(RecordIndex < RecordArray.length)
    {
        WorkOnRecord(RecordArray[RecordIndex], function() {
            WorkFunc();
        });
        RecordIndex++;
    }
};
WorkFunc();
  • Solution 3: Passing the variable as a parameter (NextStepFunc), so that I don’t need to reference WorkFunc inside (this looks so funny).

Code:

var WorkFunc = function(NextStepFunc) { // Accept function as parameter.
    if(RecordIndex < RecordArray.length)
    {
        WorkOnRecord(RecordArray[RecordIndex], function() {
            NextStepFunc(NextStepFunc); // Call function as defined by parameter.
        });
        RecordIndex++;
    }
};
WorkFunc(WorkFunc); // Pass function as variable to parameter.

But my question still stands: was my original solution legal?

  • 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-06-14T09:36:36+00:00Added an answer on June 14, 2026 at 9:36 am

    Yes. The anonymous function can access to every variable declared in parent scopes, regardless their value (that’s the standard way to make recursion with anonymous functions).

    Note that if you set the variable to null later in the parent scope, subsequents calls in the anonymous function will throw a TypeError (because null will not be callable anymore). It also means that you could even change its value to target another function (but it’s definitely not a good practice).

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

Sidebar

Related Questions

Given a class with a member template function like this one: template <typename t>
Given a function, I'd like to know whether it's a developer-defined function or a
I'd like to create a function that retrieves the post count for a given
I want to write a function like equalp, which gives #t for (equalp Xy
Given this function: > function Get-ArrayOfArrays() { (1,2),(3,4) | % { $_ } }
I got iphone/ipad to work fine with the Titanium.Media.showCamera() function. Which is great. However
I have a simplified function that looks like this: function(query) { myApi.exec('SomeCommand', function(response) {
I had no clue why this doesn't work. The following Function is created by
The concurrent.futures.Executor.map takes a variable number of iterables from which the function given is
Given a function like: function foo(myParam) if nargin<1 myParam = 'default value'; end %

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.