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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T04:18:42+00:00 2026-05-30T04:18:42+00:00

i answered one question about closures here in SO with this sample: function Constructor()

  • 0

i answered one question about closures here in SO with this sample:

function Constructor() {
    var privateProperty = 'private';
    var privateMethod = function(){
        alert('called from public method');
    };
    return {
        publicProperty: 'im public',
        publicMethod: function(){
            alert('called from public method');
        },
        getter: privateMethod
    }
}

var myObj = new Constructor();

//public
var pubProp = myObj.publicProperty;
myObj.publicMethod();
myObj.getter();

//private - will cause errors
myObj.privateProperty
myObj.privateMethod

a user commented on my answer saying:

Also, if your function explicitly returns an object it is not a good practice to call it with new because that is misleading – if using new you’d expect the result to be an instance of Constructor

i usually create objects using new. but why is it not a good practice? it seems like using new and not using new returns the same thing. what is the proper way of creating objects from closures?

  • 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-30T04:18:44+00:00Added an answer on May 30, 2026 at 4:18 am

    No, it’s not the same thing. Consider when using instanceof:

    function C1() {
        return {};
    }
    
    function C2() {
    }
    
    var c1 = new C1();
    var c2 = new C2();
    alert(c1 instanceof C1); // false; wha...?
    alert(c2 instanceof C2); // true, as you'd expect.
    

    Here’s a demo.

    So instead, create them by assigning to this, possibly with a safeguard to prevent forgotten news.

    function Constructor() {
        var privateProperty = 'private';
        var privateMethod = function() {
            alert('Called from private method');
        };
    
        this.publicProperty = "I'm public!";
        this.publicMethod = function() {
            alert('Called from public method');
        };
        this.getter = privateMethod;
    }
    

    Even better, use the prototype when possible:

    function Constructor() {
        var privateProperty = 'private';
        var privateMethod = function() {
            alert('Called from private method');
        };
    
        this.getter = privateMethod;
    }
    
    Constructor.prototype.publicProperty = "I'm public!";
    Constructor.prototype.publicMethod = function() {
        alert('Called from public method');
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I had asked a question about this earlier, but it didn't get answered right
I realize that this question has been asked and has been answered here but
Earlier today I answered this question . The question is about toggling a class
After stackoverflow answered my previous question on here about my Wiimote left/right click issue,
I had a question answered which raised another one, why following does not work?
In another question on SO I answered with code like the one below and
I half-answered a question about finding clusters of mass in a bitmap . I
I came across this answered question , but I can't seem to compile the
After having this question answered through a link to an external site , I
An earlier question was answered about getting the OrderID and the number of associated

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.