2
Systemname =
{

Question :
{
    send: function()
    {
        console.log("send");
    },

    read:   function()
    {
        console.log("read");
    },

    delete: function()
    {
        console.log("delete");
    }
},

Answer :
{
    send: function()
    {
        console.log("Answer sent");
    }
},

Person :
{
    foo: 'foo',
    bar: 'bar',


    add: function(name)
    {
        console.log('person "' + name + '" added');
    },

    remove: function(id)
    {
        console.log('person with id "' + id + '" removed');
    }
}

}

i'm learning how oop works in js and i'm a bit confused now about private methods and fields. i'd like to have some private member in the person section such as 'personCount' or 'lastAddedPerson'. if i add them like this:

Person:
{
    personCount: 0,
    lastAddedPerson: '',
    ...
}

at the beginning of the person section, the fields are public and can be called with Systemane.Person.Field.... how can i set them private? and the same for a method.

thx for your help.

roman
  • 889
  • 9
  • 16
  • You are using object literals, this is not really the way to do OOP in js. Try using functions/closures, that way you can mimic public, private, inheritance, etc... – Coin_op Mar 19 '13 at 20:35
  • There are no "private fields" in JS objects, all properties are public. If you really need privacy, you have to use closures (usually the one of the constructor function). Have a look at [this question](http://stackoverflow.com/q/13418669/1048572) – Bergi Mar 19 '13 at 20:36

3 Answers3

2

Here is one way.

function Person(n) {
   var name = n;
   this.getName = function() {
     return name;
   }
   this.setName = function(newName) {
      name = newName;
   }
}

var person = new Person('roman');
Justin Thomas
  • 5,680
  • 3
  • 38
  • 63
0

You can't have private properties or methods when you create objects using literals. In fact, there are no private properties in JavaScript, but you can achieve that in practice by using a constructor function, and declaring the private properties and methods as variables:

function Person() {
    var privteProperty = 1;
    var privateMethod = function(){}

    this.publicProperty = 2;
    this.publicMethod = function(){}
}

Then you can create an instance with:

var john = new Person();
bfavaretto
  • 71,580
  • 16
  • 111
  • 150
0

I like using a sort of factory pattern instead of new:

var Person = (function() {
   return { 
      create: function(name) {
         return (function(n) {
            var name = n;

            function getName() {
               return name;
            }

            function setName(newName) {
               name = newName;
            }

            return {
               getName: getName,
               setName: setName                    
            };
         }(name));
      }
   };   
})();

Then:

var person = Person.create("Bob");
person.getName(); //returns Bob

person.setName("Jimbo");
person.getName(); //returns Jimo

Seems complex, but is pretty simple.

Person is essentially assigned the return value of an anonymous self-invoked function. This return value has a single property called create, which is a reference to another function, which more-or-less acts like a constructor. This function also returns the return value of another anonymous self-invoked function. However, this return value is the actual instance of the object that you want. Inside this anonymous self-invoked function, you can see that I have a variable called name. This variable is private to that anonymous self-invoked function and lexically bound to the scope in which it is defined. What this means is that the value of name is preserved inside that function. Basically it hangs around even after the function is done executing. The only way you can access or modify that variable is through the getName and setName functions.

Vivin Paliath
  • 94,126
  • 40
  • 223
  • 295