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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T01:29:28+00:00 2026-06-03T01:29:28+00:00

var Gadget = function () { // static variable/property var counter = 0; //

  • 0
var Gadget = function () {
    // static variable/property
    var counter = 0;
    // returning the new implementation
    // of the constructor
    return function () {
        console.log(counter += 1);
    };
}(); // execute immediately

var g1 = new Gadget(); // logs 1
var g2 = new Gadget(); // logs 2
var g3 = new Gadget(); // logs 3

I debug this code and the var counter = 0; will not be executed during new Gadget(), and the output is 1,2,3.

var Gadget = function () {
    // static variable/property
    var counter = 0;
    // returning the new implementation
    // of the constructor
    return function () {
        console.log(counter += 1);
    }();
}; // execute immediately

var g1 = new Gadget(); // logs 1
var g2 = new Gadget(); // logs 2
var g3 = new Gadget(); // logs 3

I debug this code the var counter = 0; will be executed during new Gadget(), and the output is 1,1,1.

this demo code is in javascript pattern, private static members. I have a little trouble to understand this.

  • 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-03T01:29:30+00:00Added an answer on June 3, 2026 at 1:29 am

    What’s happening is this..

    In the first one, you have the () at the END of the function, so the function is executed, and the return statement is stored in “var Gadget”. The return statement is the constructor you have- A function. So now, when you do new Gadget(), you’re executing that function. IE, the outside function is executed right away, setting counter=0, then the inner function is returned to the variable Gadget, and every time it’s run, it increments counter.

    In the second one, however, you store the “outer” function into the “var Gadget”, but inside that function, you execute the inner function. So when you make a new Gadget, the counter is set to zero, then the return function is declared and then executed because of the () after it, hence incrementing counter, which was JUST set to 0, therefor it becomes 1.

    I hope this makes sense. When you define a function and store it in a var, the function isn’t executed yet. It’s executed when you “execute” the var, such as var(). But if you have a () after it, it is executed as a function, and whatever it RETURNS is stored into var.

    In fact, in the first code, g1, g2, and g3 will all be references to the same inner function, and in the second code, g1, g2, and g3 will be references to the Gadget function, as you expect, but then the inner function will have evaluated and return nothing.

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

Sidebar

Related Questions

var parser = new DOMParser(), d = parser.parseFromString('<?xml version=1.0?><div class=a>Hello</div>', 'application/xhtml+xml'); console.log(d.querySelector('*')); console.log(d.querySelector('div')); console.log(d.querySelector('div.a'));
var conversations = new Array(); jQuery('.CChatWindow').each(function(){ if (jQuery(this).is(:visible) && jQuery(this).attr(data-conversationid) != 0) { alert(jQuery(this).attr(data-conversationid));
var chart; var d = new Array(); function click() { d = document.getElementById('graph:hi').value; alert(d);
var ddd = Math.random() * 16; console.log((ddd & 3 | 8).toString(16)); Help me please.
function populateGroups(){ var p =1; var groupNames = new Array(); $.ajax({ type: GET, url:
var y = new w(); var x = y.z; y.z= function() { doOneThing(); x();
var h=0; var w=0; $(.extend).hover(function() { //alert(h); $(this).css({'z-index' : '999' }); $(this).addClass(hover).filter(:not(:animated)).stop() .animate({ marginTop:
var $thingy = $('.thingy'), $window = $(window); $window.on('resize', function(){ thingy.width($window.width()/12); }); Is it possible
var positions = [ 00, 01, 02, 03, 04, 05 ]; jQuery.each( positions, function(players)
i'm using the following code to submit a form: function submitfilter(){ alert(in here); var

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.