1.

What Is The Drawback Of Creating True Private Methods In Javascript?

Answer»

One of the drawbacks of creating true private methods in JAVASCRIPT is that they are very memory-inefficient, as a new copy of the method would be created for each instance.

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()
{
increaseSlary();
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);
Here each instance variable emp1, emp2, emp3 has its own copy of the increaseSalary private method.
So, as a recommendation, don’t use private methods unless it’s necessary.

One of the drawbacks of creating true private methods in JavaScript is that they are very memory-inefficient, as a new copy of the method would be created for each instance.

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()
{
increaseSlary();
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);
Here each instance variable emp1, emp2, emp3 has its own copy of the increaseSalary private method.
So, as a recommendation, don’t use private methods unless it’s necessary.



Discussion

No Comment Found