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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:36:00+00:00 2026-05-16T16:36:00+00:00

If We have var randomname = {}; randomname.attribute = ‘something’; function randomname(){ alert(randomname.attribute); }

  • 0

If We have

var randomname = {};
randomname.attribute = 'something';

function randomname(){
  alert(randomname.attribute);
}
randomname();

Will javascript throw any errors?


Update
So, We know that we cannot have a object have the same name as a function.

Why is this?

Should javascript not be able to tell what you are after by the way you call it?

  • 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-16T16:36:00+00:00Added an answer on May 16, 2026 at 4:36 pm

    It should give you a TypeError exception -for trying to invoke an object-, the behavior observed in the Firebug’s console is not right…

    FunctionDeclaration‘s are hoisted to the top of their enclosing scope, your code is actually executing in this order:

    // FunctionDeclaration is hoisted
    function randomname(){
      alert(randomname.attribute);
    }
    
    // the var has no observable effect, because
    // the randonmane identifier is already defined
    randomname = {};
    randomname.attribute = 'something';
    
    randomname(); // TypeError, randomname is not callable.
    

    When entering to an execution context, the Variable Instantiation process (aka Declaration Binding Instantiation in ES5) defines the properties on the Activation Object (AO, this is a non-reachable object that holds identifiers of variables, functions declarations and function arguments in the local scope) in the following order:

    1. FormalParameterList identifiers (for Function Code)
    2. FunctionDeclaration identifiers
    3. VariableDeclaration identifiers

    A VariableDeclaration will not overwrite an identifier has been already defined in the AO (e.g. by one of the first two steps), but the assignment will do it at run time.

    For example:

    (function (foo) {
      var foo; // doesn't affect the existing `foo` identifier
      return typeof foo;
      function foo () {}
    })('string'); // yields "function"
    

    But if an assignment is made, the value of the identifier will be replaced, for example:

    (function (foo) {
      var foo = {};
      return typeof foo;
      function foo () {}
    })('string'); // yields "object"!!
    

    In the above example, when the function is invoked, -but before the code execution- the foo identifier is set up on the Activation Object (aka Variable Object).

    First it gets assigned the value of the formal parameter, the value 'string', then all FunctionDeclaration‘s on the Function Body are examined, a function named foo is found, then the foo identifier will point to that function.

    After that, all Variable Declarations are examined, we have one with the identifier foo, but its value is respected in this time -remember that the function hasn’t been executed-.

    At this point, the function is ready to be executed, its lexical and variable environment is setup.

    The first thing that will be executed in the function, is the assignment foo = {};, which replaces the reference to the function that we had before.

    Why it behaves differently on other browsers and Firefox?

    Because:

    1. The Firebug’s console wraps your code before evaluating it.
    2. The Mozilla implementations define a Function Statement

    Since Firebug evaluates code by wrapping inside a with statement, this causes the FunctionDeclaration to be evaluated in statement context -a Mozilla’s Function Statement-.

    To show the behavior of the Function statement on Mozilla implementations, consider the following example:

    if (true) {
      function foo () { return 'true!';}
    } else {
      function foo () { return 'false??!';}
    }
    
    foo();
    

    In Mozilla you will get 'true!', while in other implementations you will get 'false??!' – even on IE-.

    That’s because the function definition was made at run-time, in Statement context (in side the true branch of the if), while in other implementations, the function declarations are evaluated at parse time.

    The above example should actually produce a SyntaxError exception on any implementation, but that doesn’t happen on any of them…

    A FunctionDeclaration should be allowed only, in global code or directly in the FunctionBody of a function.

    A lot of people use interchangeably the terms Function Declaration and Function Statement but that’s totally wrong, ECMAScript doesn’t define a Function Statement, is a non-standard feature.

    The specification has a brief note on this issue:

    NOTE: Several widely used implementations of ECMAScript are known to support the use of FunctionDeclaration as a Statement. However there are significant and irreconcilable variations among the implementations in the semantics applied to such FunctionDeclarations. Because of these irreconcilable difference, the use of a FunctionDeclaration as a Statement results in code that is not reliably portable among implementations. It is recommended that ECMAScript implementations either disallow this usage of FunctionDeclaration or issue a warning when such a usage is encountered. Future editions of ECMAScript may define alternative portable means for declaring functions in a Statement context.

    Try to run your code in the global execution context -in a simple <script> element- and you will see it crash also on Firefox:

    <script type="text/javascript"> 
      var randomname = {};
      randomname.attribute = 'something';
    
      function randomname(){
        alert(randomname.attribute);
      }
      randomname();
    </script>
    

    You can find the above example here, make sure to open the Firebug’s console and you’ll see the same error you get on other browsers.

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

Sidebar

Related Questions

Let's say you have var funct=function(a,b){ return a+b; }; console.log(funct); Is there any way
I have: var f1 = function(a){ alert(a) } var f2 = function(data, method){ method(data)
If i have: var obj={ a:function(){obj.b();}, b:function(){this.a();} }; is there any difference in calling
If I have: var test = {toString: function(){alert(evil code); return test;}}; how can I
so I have: var something = function () { return 6} var foo =
I have var marker = geo.geocode({address: adr}, geoc); where function geoc(results, status) {... I
Say I have var b = 'I am a JavaScript hacker.' How can I
I have var X={ method1 : function(){ A(); }, method2 : function(){ A(); },
i have a varable: var name = name; this will be inside an array
If I have var function_holder = { someFunction: function () { } }; How

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.