0

I recently read that using private method is bad because they are very memory inefficient because a new copy of the method would be created for each instance. In the example given, how is dispalyIncreasedSalary more efficient than increaseSalary?

var Employee = function (name, company, salary) {
  this.name = name || "";       //Public attribute default value is null
  this.company = company || ""; //Public attribute default value is null
  this.salary = salary || 5000; //Public attribute default value is null

  // Private method
  var increaseSalary = function () {
    this.salary = this.salary + 1000;
  };

  // Public method
  this.dispalyIncreasedSalary = function() {
    increaseSalary();
    console.log(this.salary);
  };
};

// Create Employee class object
var emp1 = new Employee("John","Pluto",3000);
// Create Employee class object
var emp2 = new Employee("Merry","Pluto",2000);
// Create Employee class object
var emp3 = new Employee("Ren","Pluto",2500);
pragya91
  • 52
  • 1
  • 11
  • 1
    Are you contrasting the difference between constructor defined methods and attaching a method to the prototype? https://stackoverflow.com/questions/4508313/advantages-of-using-prototype-vs-defining-methods-straight-in-the-constructor – Alex K. Feb 27 '18 at 17:53
  • No. I want to know why is it said that private methods are memory in-efficient, as compared to the methods defined with this in the above example. Sorry for ambiguous question phrasing. – pragya91 Feb 27 '18 at 18:06
  • How would you be calling this "private method"? – Bergi Feb 27 '18 at 18:11
  • If you want to "compare" the two approaches (`dispalyIncreasedSalary` vs `increaseSalary`), why does one of the functions call the other? They're not independent and can only work together, so there's not much to compare. – Bergi Feb 27 '18 at 18:14

2 Answers2

1

In the following example is this.dispalyIncreasedSalary reused for all objects?

No. You are binding it to this. So each gets it's own copy of that. That is what an instance member means. They bind to instance and not to Class/Object.

What about this.name etc. properties?

Same.

Aren't the this.propName not copied for all the instances?

If that happens all instance see the same value and that's a big NO.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • That is what I though too. The part I could not understand was : why is it said that private methods are in-efficient in terms of memory? – pragya91 Feb 27 '18 at 18:04
  • @pragya91 Because you are allocating memory for each instance. In case of public methods, they have same copy. – Suresh Atta Feb 27 '18 at 18:12
1

You are correct that it is wasteful/inefficient to duplicate what could be a shared function for each instance.

You should investigate how JavaScript prototypes work: https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Object_prototypes

So shared methods would be placed onto every object and shared like this:

Employee.prototype.dispalyIncreasedSalary = function() {
    increaseSalary();
    console.log(this.salary);
}
Randy Casburn
  • 13,840
  • 1
  • 16
  • 31
  • In the given example, is there a difference in terms of memory efficiency between increaseSalary and dispalyIncreasedSalary? – pragya91 Feb 27 '18 at 18:09
  • 1
    In your example there is very little difference in memory consumption. It is highly technical and deep, but not really significant enough to worry about. It has to do with the use of the `var` key word and directly binding with the `this` key word. This difference is not significant enough to worry about. – Randy Casburn Feb 27 '18 at 18:13
  • 1
    On the other hand, when attached to the prototype (as in my example above), there could be a significant difference based upon the number of Employee objects created with the constructor. – Randy Casburn Feb 27 '18 at 18:14