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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:31:52+00:00 2026-05-28T07:31:52+00:00

Or put another way, why does semicolon insertion fail, leaving the code below broken.

  • 0

Or put another way, why does semicolon insertion fail, leaving the code below broken.

function Foo() { }

Foo.prototype.bar = function () {
    console.log("bar");
} // <------------ missing semicolon

(function () {
    Foo.prototype.la = function () {
        console.log("la");
    };
})();

Why is the JavaScript parsing engine trying to combine Foo.prototype.bar = function () { with what’s in my closure? Is there anything I could put in that closure that would make this sensible?

I’m not advocating leaving off semicolons with the expectation that semicolon insertion will save you; I’m just wondering why (a more useful version of) the above code broke when I accidentally left one off.

  • 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-28T07:31:52+00:00Added an answer on May 28, 2026 at 7:31 am

    Think of it like this…

    Foo.prototype.bar = function () { // <-- 1. function
        console.log("bar");
    }(function () {    // <-- 2. call the 1. function, passing a function argument
        Foo.prototype.la = function () {
            console.log("la");
        };
    })();  // <-- 3. tries to invoke the return value of the 1. function, 
           //            but "undefined" was returned.
    

    I don’t like using () for IIFE. I prefer other operators.

    Foo.prototype.bar = function () {
        console.log("bar");
    }
    
    void function () {
        Foo.prototype.la = function () {
            console.log("la");
        };
    }();
    

    If we go back to the original, and have the first function return a function, you’ll see that one invoked.

    Foo.prototype.bar = function () { // <-- 1. function
    
        console.log("bar");
        return function() { alert('INVOKED'); }; // 2. return a function
    
    }(function () {    // <-- 3. call the 1. function, passing a function argument
        Foo.prototype.la = function () {
            console.log("la");
        };
    })();  // <-- 4. tries to invoke the return value of the 1. function,
           //         which will now call the returned function with the "alert()"
    

    Updated to use a unary operator as suggested by @Lasse Reichstein, as a binary operator will still evaluate its left and right operands, and return the result, which will be used for the assignment.

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

Sidebar

Related Questions

Put it another way: what code have you written that cannot fail. I'm interested
How does PHP handle client connection while doing sleeps? Or put another way ...
I feel my question is pretty dumb, or another way to put it is
[Rails] Where to put this code? I have a user1 and when another registered
Should I avoid recursion with code that runs on the iPhone? Or put another
Another clipboard question: When text is put onto the clipboard, it frequently goes in
I asked this question in another post - How do I put a <div>
If I put a header (a.h) into stdafx.h and that header includes another header
How to get average of a column in terms of another column and put
I'm working updating some legacy code that does not properly handle user input. The

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.