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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:02:13+00:00 2026-05-23T10:02:13+00:00

I have the following function var myInstance = (function() { var privateVar = ‘Test’;

  • 0

I have the following function

var myInstance =  (function() {
  var privateVar = 'Test';

  function privateMethod () {
    // ...
  }

  return { // public interface
    publicMethod1: function () {
      // all private members are accesible here
        alert(privateVar);
    },
    publicMethod2: function () {
    }
  };
})();

what’s the difference if I add a new to the function. From firebug, it seems two objects are the same. And as I understand, both should enforce the singleton pattern.

var myInstance =  new (function() {
  var privateVar = 'Test';

  function privateMethod () {
    // ...
  }

  return { // public interface
    publicMethod1: function () {
      // all private members are accesible here
        alert(privateVar);
    },
    publicMethod2: function () {
    }
  };
})();
  • 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-23T10:02:13+00:00Added an answer on May 23, 2026 at 10:02 am

    While the end result seems identical, how it got there and what it executed in is different.

    The first version executes the anonymous function with this being in the context of the window object. The second version executes the anonymous function, but this is in the context of a new empty object.

    In the end, they both return another object(your Singleton). It’s just a slight difference in execution context.

    To test this out, but an alert(this); right before the declaration of the privateVar variable.

    @Tom Squires: That’s not necessarily true and is poor practice not to declare your variables. A script with the "use strict"; directive does cause the JS engine to complain (assuming that the engine supports "use strict";

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

Sidebar

Related Questions

I have the following HTML / JS: <html> <body> <script> function foo(){ var html
I have put together the following mootools script window.addEvent('domready', function() { var shouts =
I have the following Oracle function: function get_job_no return number is V_job_no number; begin
I have the following function: var id = 10; var type = Type A;
I have the following function: var emptyFields = false; function checkNotEmpty(tblName, columns, className){ emptyFields
i have the following function function change() { var input = document.getElementById('pas'); var input2
I have the following anonymous function: (function() { var a = 1; var b
Hey, I have the following function: function getPosition() { var cLeft = $('#element').position().left; var
I have the following function: <script type=text/javascript> function changeText(elem){ var oldHTML = document.getElementById(elem).innerHTML; var
I have the following function defined: function _getUserMatches(url, mDist, ui) { var dataString =

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.