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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T23:59:59+00:00 2026-05-12T23:59:59+00:00

I watched a talk by Douglas Crockford on the good parts in Javascript and

  • 0

I watched a talk by Douglas Crockford on the good parts in Javascript and my eyes
were opened. At one point he said, something like, “Javascript is the only language where good programmers believe they can use it effectively, without learning it.” Then I realized, I am that guy.

In that talk, he made some statements that for me, were pretty surprising and insightful. For example, JavaScript is the most important programming language on the planet. Or it is the most popular language on the planet. And, that it is broken in many serious ways.

The most surprising statement he made, for me, was “new is dangerous”. He doesn’t use it any more. He doesn’t use this either.

He presented an interesting pattern for a constructor in Javascript, one that allows for private and public member variables, and relies on neither new, nor this. It looks like this:

// neo-classical constructor
var container =  function(initialParam) {
    var instance = {}; // empty object 

    // private members
    var privateField_Value = 0;
    var privateField_Name = "default";

    var privateMethod_M1 = function (a,b,c) {
        // arbitrary
    }; 

    // initialParam is optional
    if (typeof initialParam !== "undefined") {
        privateField_Name= initialParam;
    }

    // public members
    instance.publicMethod = function(a, b, c) {
        // because of closures,
        // can call private methods or
        // access private fields here. 
    };

    instance.setValue = function(v) {
        privateField_Value = v;
    };

    instance.toString = function(){
        return "container(v='" + privateField_Value + "', n='" + privateField_Name + "')";
    };

    return instance;
}


// usage
var a = container("Wallaby");
WScript.echo(a.toString()); 
a.setValue(42);
WScript.echo(a.toString()); 

var b = container();
WScript.echo(b.toString()); 

EDIT: code updated to switch to lowercase class name.

This pattern has evolved from Crockford’s earlier usage models.

Question: Do you use this kind of constructor pattern? Do you find it understandable? Do you have a better one?

  • 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-13T00:00:00+00:00Added an answer on May 13, 2026 at 12:00 am

    This looks like the non-singleton version of the module pattern, whereby private variables can be simulated by taking advantage of JavaScript’s “closures”.

    I like it (kinda…). But I don’t really see the advantage in private variables done in this way, especially when it means that any new methods added (after initialisation) do not have access to the private variables.

    Plus, it doesn’t take advantage of JavaScript’s prototypal model. All your methods and properties must be initialised EVERY time the constructor is called – this doesn’t happen if you have methods stored in the constructor’s prototype. The fact is, using the conventional constructor/prototype pattern is much faster! Do you really think private variables make the performance hit worth it?

    This kind of model makes sense with the module pattern because it’s only being initialised once (to create a pseudo-singleton), but I’m not so sure it makes sense here.

    Do you use this kind of constructor pattern?

    No, although I do use its singleton variant, the module pattern…

    Do you find it understandable?

    Yes, it’s readable and quite clear, but I don’t like the idea of lumping everything inside a constructor like that.

    Do you have a better one?

    If you really need private variables, then stick with it by all means. Otherwise just use the conventional constructor/prototype pattern (unless you share Crockford’s fear of the new/this combo):

    function Constructor(foo) {
        this.foo = foo;
        // ...
    }
    
    Constructor.prototype.method = function() { };
    

    Other similar questions relating to Doug’s views on the topic:

    • Pseudo-classical vs. “The JavaScript way”
    • Is JavaScript ‘s “new” Keyword Considered Harmful?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I watched Nicholas Zakas' talk, Speed up your Javascript , with some interest. I
I watched John Resig's Best Practices in JavaScript Library Design presentation; one slide suggested
I watched a talk in live where the person said they at work are
When I watched the Dalvik VM Internals talk, I had a question about one
I have recently watched a video of Nicholas Zakas talk about high performace scripts.
I watched the YouTube presentation Tech Talk: Linus Torvalds on git a few weeks
yesterday I watched Google IO Talk about NFC and today I'm trying to do
I just watched a Google tech talk video covering Polyworld (found here ) and
I watched the talk Three Beautiful Quicksorts and was messing around with quicksort. My
I recently watched Dav Glass' talk on YUI and node , and the server-side

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.