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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T08:35:21+00:00 2026-05-20T08:35:21+00:00

As far as I know, function foo() { aaa(); } is just var foo

  • 0

As far as I know, function foo() { aaa(); } is just var foo = function(){ aaa() } in JavaScript. So adding function foo() { bbb(); } should either overwrite the foo variable, or ignore the second definition – that’s not the point. The point is that there should be one variable foo.

So, in this example the me variable should not be correctly resolved from inside the methods and it is not in Explorer 8 🙂. I came to this example by trying to wrap them into another closure where (var) me would be, but I was surprised that it’s not necessary:

    var foo = {
        bar1 : function me() {
            var index = 1;
            alert(me);
        },
        bar2 : function me() {
            var index = 2;
            alert(me);
        }    
    };

    foo.bar1(); // Shows the first one
    foo.bar2(); // Shows the second one

Demo: http://jsfiddle.net/W5dqy/5/

  • 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-20T08:35:22+00:00Added an answer on May 20, 2026 at 8:35 am

    AFAIK function foo() { aaa(); } is just var foo = function(){ aaa() } in JavaScript.

    Not quite; they’re similar, but also quite different. JavaScript has two different but related things: Function declarations (your first example there), and function expressions (your second, which you then assign to a variable). They happen at different times in the parsing cycle and have different effects.

    This is a function declaration:

    function foo() {
        // ...
    }
    

    Function declarations are processed upon entry into the enclosing scope, before any step-by-step code is executed.

    This is a function expression (specifically, an anonymous one):

    var foo = function() {
        // ...
    };
    

    Function expressions are processed as part of the step-by-step code, at the point where they appear (just like any other expression).

    Your quoted code is using a named function expression, which look like this:

    var x = function foo() {
        // ...
    };
    

    (In your case it’s within an object literal, so it’s on the right-hand side of an : instead of an =, but it’s still a named function expression.)

    That’s perfectly valid, ignoring implementation bugs (more in a moment). It creates a function with the name foo, doesn’t put foo in the enclosing scope, and then assigns that function to the x variable (all of this happening when the expression is encountered in the step-by-step code). When I say it doesn’t put foo in the enclosing scope, I mean exactly that:

    var x = function foo() {
        alert(typeof foo); // alerts "function" (in compliant implementations)
    };
    alert(typeof foo);     // alerts "undefined" (in compliant implementations)
    

    Note how that’s different from the way function declarations work (where the function’s name is added to the enclosing scope).

    Named function expressions work on compliant implementations. Historically, there were bugs in implementations (early Safari, IE8 and earlier). Modern implementations get them right, including IE9 and up. (More here: Double take and here: Named function expressions demystified.)

    So, in this example the me variable shoudl not be corectly resolved from inside the methods

    Actually, it should be. A function’s true name (the symbol between function and the opening parenthesis) is always in-scope within the function (whether the function is from a declaration or a named function expression).

    NOTE: The below was written in 2011. With the advances in JavaScript since, I no longer feel the need to do things like the below unless I know I’m going to be dealing with IE8 (which is very rare these days).

    Because of implementation bugs, I used to avoid named function expressions. You can do that in your example by just removing the me names, but I prefer named functions, and so for what it’s worth, here’s how I used to write your object:

    var foo = (function(){
        var publicSymbols = {};
    
        publicSymbols.bar1 = bar1_me;
        function bar1_me() {
            var index = 1;
            alert(bar1_me);
        }
    
        publicSymbols.bar2 = bar2_me;
        function bar2_me() {
            var index = 2;
            alert(bar2_me);
        }
    
        return publicSymbols;
    })();
    

    (Except I’d probably use a shorter name than publicSymbols.)

    Here’s how that gets processed:

    1. An anonymous enclosing function is created when the var foo = ... line is encountered in the step-by-step code, and then it is called (because I have the () at the very end).
    2. Upon entry into the execution context created by that anonymous function, the bar1_me and bar2_me function declarations are processed and those symbols are added to the scope inside that anonymous function (technically, to the variable object for the execution context).
    3. The publicSymbols symbol is added to the scope inside the anonymous function. (More: Poor misunderstood var)
    4. Step-by-step code begins by assigning {} to publicSymbols.
    5. Step-by-step code continues with publicSymbols.bar1 = bar1_me; and publicSymbols.bar2 = bar2_me;, and finally return publicSymbols;
    6. The anonymous function’s result is assigned to foo.

    These days, though, unless I’m writing code I know needs to support IE8 (sadly, as I write this in November 2015 it still has significant global market share, but happily that share is plummetting), I don’t worry about it. All modern JavaScript engines understand them just fine.

    You can also write that like this:

    var foo = (function(){
        return {
            bar1: bar1_me,
            bar2: bar2_me
        };
    
        function bar1_me() {
            var index = 1;
            alert(bar1_me);
        }
    
        function bar2_me() {
            var index = 2;
            alert(bar2_me);
        }
    })();
    

    …since those are function declarations, and thus are hoisted. I don’t usually do it like that, as I find it easier to do maintenance on large structures if I do the declaration and the assignment to the property next to each other (or, if not writing for IE8, on the same line).

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

Sidebar

Related Questions

What I think I know so far: so $this-> is to access a function/var
As far as variable naming conventions go, should iterators be named i or something
For as far as I know, a method is compiled into a function like
As far as I know, foreign keys (FK) are used to aid the programmer
As far as I know, Flash has to pass info off to another external
As far as I know, in gcc you can write something like: #define DBGPRINT(fmt...)
As far as i know it is not possible to do the following in
As far as I know (not much I'll admit), the currently popular programming paradigms
As far as I know, there's no way to use {% include %} within
The problem I'm running into is that as far as I know the delete

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.