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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T01:10:03+00:00 2026-05-30T01:10:03+00:00

This question is more on javascript principle. function done(){ console.log(‘done defined with `function done(){

  • 0

This question is more on javascript principle.

function done(){ console.log('done defined with `function done(){ ...`'); }
var done = function(){ console.log('done defined with `var done = ...`'); }
done = function(){ console.log('without `var`, just `done = ...`'); }

If defined right inside <script> tags, will they all do the same thing, right?

But if I place them in a closure (function(){ function definintion goes here }()) will any of these three types override either the globally defined function done() or any other done() functions that are defined inside their respective closures?

If the question above doesn’t make sense, here’s to rephrasing;

  • is the following code supposed to do the same thing in any JS runtime?
  • eval-ing code anywhere executes that particular code within the context or the global scope?
  • how can a setTimeout call be configured so that the code between its “quotes” executes to inside the scope where that particular setTimeout has been called (please see second timeout inside for below)? I mean is there any other way besides defining window.blabla functions and telling them to delete themselves after they run?

    function done(d){ console.log('cha cha cha: '+d); }
    setTimeout( function(){ done(2); }, 3500 );
    
    for(i=0; i<10; i++){
        (function(){
            done = function(x){ console.log('done #'+i+' sais: '+x); }
            setTimeout(function(){ done(i*2); },2500);
            setTimeout(function(){ done(i*2); }.toString()+'(); ',2500);
        }());
    }
    
  • 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-30T01:10:05+00:00Added an answer on May 30, 2026 at 1:10 am

    For the initial question on general behaviour:

    • var done = and function done do basicaly the same thing. They will shadow the outer definition in the inner scope but they will not replace it on the outer scope.

    • done = will set the corresponding done variable in scope or will create a global variable if such a variable does not exist and the program is not running in strict mode.

    • At a global level, outside any function, var done = and done = should work the same, but they work differently in IE if you try to use the variable in another script tag (stick to var = – its better anyway).

    As for the very evil setTimeout and eval questions:

    • Yes, I guess this kind of stuff should be standardized enough to work the same everywhere. I would still test it anyway. (Or you could use a different solution, given how evil eval is)

    • eval runs code in the current scope (using deep black magic to do so). If you want to run code on the global scope you can use new Function instead.

    • In order to have the settimeout run the string in the curent scope you can add the eval yourself:

      var done = function(d){ console.log('outer done', d); };
      
      (function(){
          var done = function(x){ console.log('inner done', x); };
      
          setTimeout(function(){ done(1); }, 200);           //inner done
          setTimeout('done(2)', 400);                        //outer done
          setTimeout(function(){ eval('done(3)'); }, 600);   //inner done
      }());
      
    • Once again, why are you eval-ing things in the setTimeouts? This all sounds profoundly evil!

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

Sidebar

Related Questions

This question is in fact more about html+Javascript but Twitter is cited because I
This question is more UI/Design-ish than hard-core programming is. Background: I've been coding in
EDIT: This question is more about language engineering than C++ itself. I used C++
I had asked this question Adding more attributes to LINQ to SQL entity Now,
I suppose this question is more general than working with COM components. I have
This is a code review question more then anything. I have the following problem:
This is related to my previous question More than 1 Left joins in MSAccess
This question was asked to me in an interview. Suppose char *p=malloc(n) assigns more
In relation to this question ( Efficient hashCode() implementation ) I have one more
In this question , I mentioned my assumption that rubyforge gems are more official,

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.