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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:38:06+00:00 2026-06-14T20:38:06+00:00

Considering object creation patterns with private properties, one way to do is : function

  • 0

Considering object creation patterns with private properties, one way to do is :

function MyStack (){
    var list = [],
        index = 0;

    this.push =  function(val){ 
        return list[index++] = val;
    };
    this.pop = function(){// ...}
}

var stack1 = new MyStack();       stack1.push(5);
var stack2 = new MyStack();       stack2.push(11);

Problem with this: Every instance of Stack has it’s own copy of methods ‘push’ and ‘pop’.

Another way for implementing constructor method is:

function MyStack(){ 
    this.list = []; 
    this.index = 0;
}
MyStack.prototype = {
    insert: function(val){
            return this.list[this.index++] = val;
        },
    pop:function(){//...}
}

Problem here: We lose the privacy of list and index.

Is there a way, such that we can have both methods reuse among instances and privacy of properties ?

I understand that we can have this for methods that don’t operate on any state of the object, but I am talking more about those methods that do operate on the 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-14T20:38:07+00:00Added an answer on June 14, 2026 at 8:38 pm

    Yes. I’ve edited this code so it’s actually fully functional as you had intended it to work. It seems a bit redundant to me, but, it does provide you the ability to provide a public interface, but to keep your variables private and control the way the user interacts with them.

    function MyStack(){ 
        var list = []; 
        var index = 0;
    
        this.getIndex = function(){
            return index;
        }
        this.setIndex = function(val){
            index = val;
        }
        this.list = function(val){
            if(val){
               // setter if a value was provided. Illustrating how you can control
               // index, which I assume is the point of having these things private 
               // to begin with
               return list[this.setIndex(this.getIndex() + 1)] = val;
            }
    
            // always return list - acts like a getter
            return list;
        }
    }
    MyStack.prototype = {
        insert: function(val){
                return this.list(val);
            },
        pop:function(){}
    }
    
    var stack1 = new MyStack();       
    stack1.insert(5);
    var stack2 = new MyStack();       
    stack2.insert(11);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Considering this part of a Java class, private List<Object> components = new ArrayList<Object>(); private
Considering this code with 3 differents function call semantics: void f(void){ puts(OK); } int
AFAIK, all types are derived from System.Object class. Considering this, how can I calculate
I am considering using JavaScript object as a dictionary. var dict = {} dict['a']
I am not asking if this is okay: Object.prototype.method = function(){}; This is deemed
Considering the boundaries of a List are known, does .Last() enumerate the collection? I
Considering it's a common issue, what could be the source of this error if
If this has already been asked, please link and close this one. I'm currently
I'm playing around with a user interface that will allow the creation of one
I am considering the design of an interpreter for Python like object oriented language

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.