0

I read a few articles and questions heres but I still don't understand how to achieve this. The following snippet will help me to explain what I am trying to do:

function Employee(id, name, email){
    var _id = id;
    var _name = name;
    this._email = email;
}

Employee.prototype.getId = function(){
    return _id;
}

Employee.prototype.getName = function(){
    return this._name;
}

Employee.prototype.getEmail = function(){
    return this._email;
}

I went on to create a couple of instances:

var emp1 = new Employee(1,'Brendan Eich','brendaneich@gmail.com');

When I am using var for variables like id & name they behave like private members but I cannot access them from methods like getId() and getName() which are defined on Employee.prototype.

On the other hand declaring email with this._email = email works all fine but this doesn't preserve privacy as i can access it asan object property directly without the need of an access method.

What i want to know:

  1. What is the best way to declare a private variable when using constructor functions?
  2. If i use var to declare variables then where will it reside in the object?
Maria Ines Parnisari
  • 16,584
  • 9
  • 85
  • 130
BeingSuman
  • 3,015
  • 7
  • 30
  • 48
  • 1
    The best practice is to set them on `this` with an underscore prefix, like you’re doing with `this._email`. Anything you declare with `var` in the constructor will be inaccessible from outside its scope; those variables do not reside “in” the object at all. – Ry- Jun 01 '17 at 02:29
  • If you want to enforce privacy you can define the getter methods (and setter methods, if required) within the constructor where they have access to the local variables. `this.getId = function() { return _id }` – nnnnnn Jun 01 '17 at 02:35
  • 1
    javascript has many features "native" to the language but unfortunately privacy isn't one of those. The overall consensus for developers is to simply prefix any private identifier with an underscore. This tells other develops that they shouldn't touch the identifer but it doesn't prevent them from do that either. [This article by philipwalton explains a way of getting real privacy working in javascript (where external devs can't touch private members)](https://philipwalton.com/articles/implementing-private-and-protected-members-in-javascript/). – Rico Kahler Jun 01 '17 at 02:41
  • [and this article from douglas crockford explains more native privacy techniques in ES5 terms](http://javascript.crockford.com/private.html) – Rico Kahler Jun 01 '17 at 02:43
  • @Ryan adding to the curiosity, where do variables declared with `var` reside ? – BeingSuman Jun 01 '17 at 02:43
  • 1
    @BeingSuman [read crockford's article I referred to](http://javascript.crockford.com/private.html). It's old (from 2001) but it explains how variables declared in the constructor behave in ES3-ES5 terms. – Rico Kahler Jun 01 '17 at 02:47
  • @BeingSuman: Variables can’t really be said to reside anywhere. They contain values; if those values are accessible at some point in the program, they have to be “in memory”. The important thing is that they’re accessible from within their scope and inaccessible from outside. – Ry- Jun 01 '17 at 02:52
  • 1
    There’s also no point in having “real” privacy (you can get it with `WeakMap`s, but why bother?); don’t use hacks like assigning functions in the constructor to try to get it. – Ry- Jun 01 '17 at 02:53

2 Answers2

2

Try this:

function Employee(id, name, email){
var _id = id; //private 
var _name = name; //private
this._email = email; //public


  var fooPrivate = function(){
      return ;
  }

  this.getId = function(){ //Public function
    fooPrivate(); //I can only be called inside other member functions
    return _id;
  }


  this.getName = function(){ //Public function
    //Note that you don't use this._name for private variables
    return _name;
  }


  this.getEmail = function(){ //Public function
   return this._email;
  }

}

What is the best way to declare a private variable when using constructor functions?

If i use var to declare variables then where will it reside in the object?

  function Employee{ //private only accessible here... }
Jorge González
  • 2,898
  • 1
  • 9
  • 12
2

Variables declared with var only reside in the scope of the function.

To get private properties, the best you can do is to name them so they are obviously private and hope that people don't come along and mutate them. Underscores are a decent way to do that.

You can also pull fancy tricks with functions and closures, but at the end of the day, they won't be properties.

James Kraus
  • 3,349
  • 20
  • 27