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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:09:39+00:00 2026-06-11T22:09:39+00:00

I understand basic JavaScript pseudo-classes: function Foo(bar) { this._bar = bar; } Foo.prototype.getBar =

  • 0

I understand basic JavaScript pseudo-classes:

function Foo(bar) {
    this._bar = bar;
}

Foo.prototype.getBar = function() {
    return this._bar;
};

var foo = new Foo('bar');
alert(foo.getBar()); // 'bar'
alert(foo._bar); // 'bar'

I also understand the module pattern, which can emulate encapsulation:

var Foo = (function() {
    var _bar;

    return {
        getBar: function() {
            return _bar;
        },
        setBar: function(bar) {
            _bar = bar;
        }
    };
})();

Foo.setBar('bar');
alert(Foo.getBar()); // 'bar'
alert(Foo._bar); // undefined

But there are un-OOP-like properties to both of these patterns. The former does not provide encapsulation. The latter does not provide instantiation. Both patterns can be modified to support pseudo-inheritance.

What I’d like to know is if there is any pattern that allows:

  • Inheritance
  • Encapsulation (support for “private” properties/methods)
  • Instantiation (can have multiple instances of the “class”, each with its own state)
  • 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-11T22:09:40+00:00Added an answer on June 11, 2026 at 10:09 pm

    what about this :

    var Foo = (function() {
        // "private" variables 
        var _bar;
    
        // constructor
        function Foo() {};
    
        // add the methods to the prototype so that all of the 
        // Foo instances can access the private static
        Foo.prototype.getBar = function() {
            return _bar;
        };
        Foo.prototype.setBar = function(bar) {
            _bar = bar;
        };
    
        return Foo;
    })();
    

    And now we have instantiation, encapsulation and inheritance.
    But, there still is a problem. The private variable is static because it’s shared across all instances of Foo. Quick demo :

    var a = new Foo();
    var b = new Foo();
    a.setBar('a');
    b.setBar('b');
    alert(a.getBar()); // alerts 'b' :(    
    

    A better approach might be using conventions for the private variables : any private variable should start with an underscore. This convention is well known and widely used, so when another programmer uses or alters your code and sees a variable starting with underscore, he’ll know that it’s private, for internal use only and he won’t modify it.
    Here’s the rewrite using this convention :

    var Foo = (function() {
        // constructor
        function Foo() {
            this._bar = "some value";
        };
    
        // add the methods to the prototype so that all of the 
        // Foo instances can access the private static
        Foo.prototype.getBar = function() {
            return this._bar;
        };
        Foo.prototype.setBar = function(bar) {
            this._bar = bar;
        };
    
        return Foo;
    })();
    

    Now we have instantiation, inheritance, but we’ve lost our encapsulation in favor of conventions :

    var a = new Foo();
    var b = new Foo();
    a.setBar('a');
    b.setBar('b');
    alert(a.getBar()); // alerts 'a' :) 
    alert(b.getBar()); // alerts 'b' :) 
    

    but the private vars are accessible :

    delete a._bar;
    b._bar = null;
    alert(a.getBar()); // alerts undefined :(
    alert(b.getBar()); // alerts null :(
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I understand that the basic for...of syntax in JavaScript looks like this: for (let
This is a very basic question. I'm learning Javascript, and I'm fairly new to
Hi I'm just going over some basic javascript and I came across this piece
I'm a comer at JavaScript, and just learning some basic stuff (function, variable, etc)
I was trying to write this basic JavaScript program which changes the background color
I want to understand the basic differences clearly between JavaScript object and JSON string.
I can understand basic javascript and jquery but I'm having a hard time understanding
So, I am pretty new to web programming and I just understand the basic
I understand this question may sound very basic but I have been trying to
I am trying to better understand basic concepts in OOP. What are static and

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.