Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to have Javascript Object creation pattern with Reusable methods and private properties?

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.

like image 311
sbr Avatar asked Sep 28 '12 18:09

sbr


People also ask

What three ways is there to create objects in JavaScript?

There are different ways to create new objects: Create a single object, using an object literal. Create a single object, with the keyword new . Define an object constructor, and then create objects of the constructed type.

Can JavaScript objects have methods?

Objects in JavaScript are collections of key/value pairs. The values can consist of properties and methods, and may contain all other JavaScript data types, such as strings, numbers, and Booleans.

What are the two kinds of objects properties we can have in JavaScript?

JavaScript objects have two types of properties: data properties and accessor properties.

Which are the different ways to enumerate all properties of an object in JavaScript?

Every property in JavaScript objects can be classified by three factors: Enumerable or non-enumerable; String or symbol; Own property or inherited property from the prototype chain.


2 Answers

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);
like image 199
netpoetica Avatar answered Oct 12 '22 22:10

netpoetica


You should check out John Resig's Simple Javascript Inheritance. It is a great read, and it has been extended to provide support for privates, aptly called Privates.js;

like image 24
Ryan Wheale Avatar answered Oct 12 '22 22:10

Ryan Wheale