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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T06:04:56+00:00 2026-06-05T06:04:56+00:00

When I have some code I need to execute more than once I wrap

  • 0

When I have some code I need to execute more than once I wrap it up in a function so I don’t have to repeat myself. Sometimes in addition there’s a need to execute this code initially on page load. Right now I do it this way:

function foo() {
   alert('hello');
}

foo();

I would rather do it this way:

(function foo() {
   alert('hello');
})();

Problem is, this will only execute on page load but if I try to call it subsequent times using foo() it won’t work.

I’m guessing this is a scope issue but is there any way to get self executing functions to work upon getting called later?

  • 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-05T06:04:59+00:00Added an answer on June 5, 2026 at 6:04 am

    If your function doesn’t rely on a return value, you can do this…

    var foo = (function bar() {
       alert('hello');
       return bar;
    })();   // hello
    
    foo();  // hello
    

    This uses the local reference bar in the named function expression to return the function to the outer foo variable.


    Or even if it does, you could make the return value conditional…

    var foo = (function bar() {
       alert('hello');
       return foo ? "some other value" : bar;
    })();   // hello
    
    alert( foo() );  // hello --- some other value
    

    or just manually assign to the variable instead of returning…

    var foo; 
    (function bar() {
       alert('hello');
       foo = bar;
    })();   // hello
    
    foo();  // hello
    

    As noted by @RobG, some versions of IE will leak the identifier into the enclosing variable scope. That identifier will reference a duplicate of the function you created. To make your NFE IE-safe(r), you can nullify that reference.

    bar = null;
    

    Just be aware that the identifier will still shadow an identifier with the same name up the scope chain. Nullifying won’t help that, and local variables can not be deleted, so choose the NFE name wisely.

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

Sidebar

Related Questions

I need to integrate some legacy 32-bit code - for which I don't have
we have some C++ code that we need to create a make file in.
I have some code in my error handler I need to test against a
I have some really nice Python code to do what I need to do.
I have a Maven project within which I need execute two code generation steps.
I need to execute some code after each deserialization is done on a POJO's
Every time when I need to execute more than one query at work by
I have some code that seems to execute improperly when it is serialized and
I have some code which populates a hashtable with a question as the key
I have some code using a System.Transactions.TransactionScope , that creating a new instance of

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.