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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:56:50+00:00 2026-05-20T10:56:50+00:00

I know you can achieve something as privateness in JavaScript by using closures and

  • 0

I know you can achieve something as “privateness” in JavaScript by using closures and Immediate Invoked Functions.

But what if I need full featured prototyping? There simply is no way I know of of having private members in an object’s prototype. If I used privileged methods I can have private variables and public methods but I lose the option of prototyping.

Douglas Crockford “forbids” the use of dangling (putting an underscore in front of an identifier to indicate that it is not part of the public interface).

But is it that bad to use it? Since there is no way to make it real private.

What is your opinion about this? How do you handle it?

  • 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-20T10:56:51+00:00Added an answer on May 20, 2026 at 10:56 am

    Well at first, you don’t really lose the prototyping-effect when using a functional-inheritance pattern. I just assume you’re talking about The good parts, crockford also introduced a pretty easy and effective way to have shared variables for that pattern aswell. Which basically looks like:

    var Human = (function(my, priv) {
        var my   = my || {},
            priv = priv || {};
    
        my.privatedata = "foobar";
    
        priv.walk = function() {
            console.log('walking');
            return priv;
        };
        priv.talk = function() {
            console.log('blah blah');
            return priv;
        };
    
        return priv;
    }());
    
    var Andy = (function(my, priv) {
        var my   = my || {},
            priv = Human(my, priv);
    
        priv.SpecialAndyThing = function() {
            console.log('bloggin at typeofnan.com');
            return priv;
        };
    
        return priv;
    }());
    
    var myAndy = Andy();
    myAndy.talk().SpecialAndyThing();
    

    You can even simply extend this techniqe to have somekind of super methods. Using cryptic variable-conventions like underscores or whatnot is just bad practice in general. Its confusing since nobody just knows what is going on there (probably that argument fails if you’re the only one using the codebase).


    However, ECMAscript Edition 5 introduces some goodys to have more “private” members in a prototype chain. One important method for that is .defineProperty, where you can define a property which does not “shallow” through. Would look like:

    var Human = {};
    
    Object.defineProperty(Human, 'privateStuff', {
        value:      'secret',
        enumerable: false
    });
    

    Now, the property privateStuff is not visible for an object that inherits from Human‘s prototype chain. Anyway, this stuff requires Javascript 1.8.5 and is only available in cutting edge browsers for now. See https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/defineProperty

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

Sidebar

Related Questions

Does anyone know how I can achieve the following effect in OpenGL: Change the
Anyone know of a control for asp.net that can achieve this either server or
How can I achieve a cool looking photo gallery page? I don't know Flash,
I know I can do most of this by hacking Trac and using Git
I know you can look at the row.count or tables.count, but are there other
I know you can do redirection based on the domain or path to rewrite
I know I can programatically make the taskbar item for a particular window start
I know I can call the GetVersionEx Win32 API function to retrieve the Windows
I know I can figure out the name of the method as its being
I know I can specify one for each form, or for the root form

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.