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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:30:33+00:00 2026-06-13T23:30:33+00:00

Why if we run the following javascript code: var foo = { bar: function

  • 0

Why if we run the following javascript code:

        var foo = {
           bar: function () {
               alert('s');
           }
       }

      //1. foo.bar();
      //2. var a = foo.bar;
      //3. var b = (foo.bar);          
       (foo.bar)();

we’ll get an error message “foo is undefined”, but if we uncomment 1. or 2. or 3. line we’ll get alert ‘s’? I know that it’s about Reference type and GetValue(), but I don’t really understand the point.

Thanks

  • 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-13T23:30:35+00:00Added an answer on June 13, 2026 at 11:30 pm

    It’s because you don’t have a semicolon before the (foo.bar).

    This causes the (...) to be interpreted as a function call operator, so it’s trying to invoke the previous expression as though it was a function.

      var foo = {
           bar: function () {
               alert('s');
           }
       }(foo.bar) // <-- It sees it like this
    

    If you terminate the previous with a ;, it’ll work.

       var foo = {
           bar: function () {
               alert('s');
           }
       }; // <--- right here
    
      //1. foo.bar();
      //2. var a = foo.bar;
      //3. var b = (foo.bar);          
       (foo.bar)();
    

    So the reason it worked when the other lines were uncommented is that they are terminated.

    If you want to exclude semicolons (which is what I do), you’ll generally be safe if you follow these rules:

    • Never begin a new line with a ( unless you have a ; before it
    • Never begin a new line with a [ unless you have a ; before it

    Whenever I start a line with one of those characters, I put a ; before it so I know the previous statement has been terminated.

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

Sidebar

Related Questions

Following javascript code: <script type=text/javascript charset=utf-8> function goForLogin() { var xmlhttp; xmlhttp=new XMLHttpRequest(); xmlhttp.open(POST,/account/login,true);
In the following javascript code: function foo() { foo.val = foo.val || 'no val';
I have the following javascript code: function delete_draft(id, name) { var text = 'Are
when I trying to run following code. var result = from c in db.brand
Hi I have following JavaScript code that I am trying to run. My aim
I have the following javascript: function graphStreamPublish(){ FB.api('/me/feed', 'post', { message : 1, link
I have a problem with the following javascript code. Basically I want to run
I run the following JavaScript code which gives my the list of parameters from
Onkeydown , I run the following JavaScript: function ThisOnKeyDown(el) { if (el.title == 'textonly')
I run PHP in JavaScript files, e.g.... var = '<?php /*some code*/ ?>';). 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.