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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:24:17+00:00 2026-05-28T13:24:17+00:00

This is not a how to question. I want to discuss two possible ways

  • 0

This is not a “how to” question. I want to discuss two possible ways of instantiation in JavaScript to find pros and cons of each one.

First one:

function Counter() {
  this.counter = 0;
}

Counter.prototype.count = function() {
  this.counter += 1;
  return this.counter;
};

var counter = new Counter();

console.log("Counter: " + counter.count());

And the second:

var counter = new (function() {
  var counter = 0;
  this.count = function() {
    counter += 1;
    return counter;
  }
})();

console.log("Counter: " + counter.count());

IMPORTANT NOTE: counter is supposed to be instantiated only once.

The first one looks more structured but the second does not clutter the namespace with unnecessary Counter function.

What do you think? Should I consider these two pieces of code equivalent? Am I missing something?


BTW, I use the 2nd way at the moment. The reason I ask this question is that JSHint states that it’s a “Weird construction.” and proposes to delete new which apparently breaks the code.

  • 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-28T13:24:18+00:00Added an answer on May 28, 2026 at 1:24 pm

    The only difference is that counter can be modified by an external source, at the first method (given the fact the both “classes” will only be initialized once).

    At the first method, counter is “publicly” visible and editable property.
    At the second method, counter is a local variable, which cannot be modified by anything outside the function.

    counter = new Counter();
    counter.count(); // 1
    counter.count = 9001; // Not intended?
    counter.count(); // 9002
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just want to clarify one thing. This is not a question on which
I am not even sure how to ask this question. I want something that
This is not a question about how to sign an .apk file. I want
Before anything, this is not necessarily a question.. But I really want to know
This is NOT a question on plain old boring customization; I actually want to
This is not a question about a specific framework. I am using plain php
This is not a question of premature optimization per se. On the garbage collector
This is not a question about jQuery, but about how jQuery implements such a
Okay this is not a question of how to get all uniques or How
Disclaimer: this is not a question about how to install asp.net or an application

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.