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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:31:40+00:00 2026-05-23T15:31:40+00:00

Normally, I see function closure achieved by the form var closedF = (function() {

  • 0

Normally, I see function closure achieved by the form

var closedF = (function()
{
    return function(){/* return value */}
})();

so that calling closedF() returns the return value from the inner function. But I want to create closedF using a function declaration (the above is a function expression) so that it is defined at parse time. I.e.

function closedF()
{
    return function(){/* return value */}
}

but this doesn’t work because when calling closedF(), it returns the inner function as opposed to the return value of the inner function. Note: with the above declaration I could use closedF()(), but that’s just inelegant.

Is this possible?

p.s. As is usually the case there are many ways for me to solve my particular programming problem, but I actually want someone to show me that closed-over functions aren’t “second-class citizen” functions in JS.

  • 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-23T15:31:40+00:00Added an answer on May 23, 2026 at 3:31 pm

    Your problem has not necessarily anything to do with function expression vs function declaration.

    In the both cases you showed, you don’t get the return value of the inner function.

    You could get it by just calling the inner function and return it’s value:

    function closedF() {
        return (function(){
            /* return value */
        }()); // <- calling the inner function here
    }
    

    The disadvantage is that the inner function will always be defined anew when calling the outer function. You could prevent this by caching the inner function as a property of the outer function:

    function closedF() {
        var func = closedF.__func ||  (closedF.__func = function(){
            /* return value */
        });
        return func();
    }
    

    However, this might be more confusing than helpful.

    Easier is to execute the outer function immediately and assign the returned function to a variable, as @James Long shows in his answer.

    Update:

    You could also do this:

    (function() {
        window.closedF = function() {
    
        };
    }());
    

    though both are function expressions

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

Sidebar

Related Questions

Normally you create a function using cfscript like: <cfscript> function foo() { return bar;
Delphi Xe. In module Windows.pas I see one of methods: function InterlockedExchangeAdd(Addend: PLongint; Value:
I need to see the queries submitted to a PostgreSQL server. Normally I would
Normally, in Delphi one would declare a function with a variable number of arguments
Normally when you update an object in linq2sql you get the object from a
We are using impersonation by using the LogonUser function from advapi32.dll along with the
Quick check to see if anyone has or knows of a T-SQL function capable
That may sound a little confusing. Basically, I have a function CCard newCard() {
Normally in an ASP.NET view one could use the following function to obtain a
In C, I normally use the getch() function to wait for a key to

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.