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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:07:42+00:00 2026-05-24T17:07:42+00:00

I can create a recursive function in a variable like so: /* Count down

  • 0

I can create a recursive function in a variable like so:

/* Count down to 0 recursively.
 */
var functionHolder = function (counter) {
    output(counter);
    if (counter > 0) {
        functionHolder(counter-1);
    }
}

With this, functionHolder(3); would output 3 2 1 0. Let’s say I did the following:

var copyFunction = functionHolder;

copyFunction(3); would output 3 2 1 0 as above. If I then changed functionHolder as follows:

functionHolder = function(whatever) {
    output("Stop counting!");

Then functionHolder(3); would give Stop counting!, as expected.

copyFunction(3); now gives 3 Stop counting! as it refers to functionHolder, not the function (which it itself points to). This could be desirable in some circumstances, but is there a way to write the function so that it calls itself rather than the variable that holds it?

That is, is it possible to change only the line functionHolder(counter-1); so that going through all these steps still gives 3 2 1 0 when we call copyFunction(3);? I tried this(counter-1); but that gives me the error this is not a function.

  • 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-24T17:07:43+00:00Added an answer on May 24, 2026 at 5:07 pm

    Using Named Function Expressions:

    You can give a function expression a name that is actually private and is only visible from inside of the function ifself:

    var factorial = function myself (n) {
        if (n <= 1) {
            return 1;
        }
        return n * myself(n-1);
    }
    typeof myself === 'undefined'
    

    Here myself is visible only inside of the function itself.

    You can use this private name to call the function recursively.

    See 13. Function Definition of the ECMAScript 5 spec:

    The Identifier in a FunctionExpression can be referenced from inside the FunctionExpression’s FunctionBody to allow the function to call itself recursively. However, unlike in a FunctionDeclaration, the Identifier in a FunctionExpression cannot be referenced from and does not affect the scope enclosing the FunctionExpression.

    Please note that Internet Explorer up to version 8 doesn’t behave correctly as the name is actually visible in the enclosing variable environment, and it references a duplicate of the actual function (see patrick dw‘s comment below).

    Using arguments.callee:

    Alternatively you could use arguments.callee to refer to the current function:

    var factorial = function (n) {
        if (n <= 1) {
            return 1;
        }
        return n * arguments.callee(n-1);
    }
    

    The 5th edition of ECMAScript forbids use of arguments.callee() in strict mode, however:

    (From MDN): In normal code arguments.callee refers to the enclosing function. This use case is weak: simply name the enclosing function! Moreover, arguments.callee substantially hinders optimizations like inlining functions, because it must be made possible to provide a reference to the un-inlined function if arguments.callee is accessed. arguments.callee for strict mode functions is a non-deletable property which throws when set or retrieved.

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

Sidebar

Related Questions

I would like to dynamically create a set of labels in an assembly function
I can create the following and reference it using area[0].states[0] area[0].cities[0] var area =
I'm attempting to create a recursive function that outputs a vector of strings that
I can create a menu item in the Windows Explorer context menu by adding
I can create a literal long by appending an L to the value; why
I can create a contact that is not mail enabled, but how do I
How can create a view with mutiple images, something similiar to the photo app
One can create an anonymous object that is initialized through constructor parameters, such as
How can create a New user in ORACLE with full access (alter, delete, select,
I can create an array of buttons in Windows Form but how can i

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.