What is the drawback of creating true private methods in JavaScript?

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 emp1emp2emp3 has its own copy of the increaseSalary private method.

Asked on January 18, 2019 in Javascript.
Add Comment


  • 6 Answer(s)

    It is certainly possible to create private functions, by encapsulating them in a scope that you simply choose not to pass the out of in any way. But to be a method, it must be accessible as a property of an object, and there is no way to define a property of an object that is accessible from other methods of that object (or possibly its descendants), but not from anywhere else.

    The closest you can come is defining methods whose names are private symbols, available in the scope where other methods of the same object are defined but not elsewhere. In that case, the major drawback is that you have to go through the complication of calling that method via a symbol lookup (e.g., this[methodSymbol]()) rather than the typical dot-notation (e.g., this.method()). But that’s not really a “true” private method, because someone else could always discover and call it with Object.getOwnPropertySymbols().

    If by “creating true private methods in JavaScript”, you actually mean altering the language specification so that it is possible to create private methods, the drawback is having to deal with a complete restructuring of the language’s core semantics to support class-based objects, while still maintaining backwards compatibility with existing code that uses objects with prototype-based inheritance.

    Answered on January 19, 2019.
    Add Comment

    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.

    Answered on January 20, 2019.
    Add Comment

    JavaScript is primarily a language based around objects. Arrays and functions are both objects. Methods are actions that objects can perform. In order to understand how to work with private methods in JavaScript you need to know these fundamentals. Objects are filled with values of data types like strings, numbers and booleans.

    When values become a function they are then considered methods. In order to understand private methods, we first have to look at the distinction between a public method.

    Public Versus Private Methods

    Objects are produced by constructors which initialize the actions inside the function. For a public method, all members inside the object are public. This means that any function can be easily accessed, changed or deleted. One of the first ways of creating a public method is through using this constructor syntax.

    1
    2
    3
    4
    5

    function Object(paramType) {
    this.value = paramType;
    }

    Using this constructor we’ve initialized a public method. The following constructor will show the initialization of a private method. We are going to create three different variables inside the private method. These include a paramType, secretValue integer and that. These are all attached inside the object and can’t be manipulated with from the outside. Private methods are internal functions of the overall constructor templates we’ve made. Compare the two of these public and private methods to see the difference.

    1
    2
    3
    4
    5
    6
    7

    function Object (paramType) {
    this.value = paramType;
    var secretValue = 5;
    var that = this;
    }

    We’ll lay out a couple more examples to show how this works then we’ll look at the common pitfalls programmers fall into when working with private methods.

    Answered on January 27, 2019.
    Add Comment

    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.

    Answered on January 28, 2019.
    Add Comment

    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.

    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.

    Answered on February 4, 2019.
    Add Comment

    One of the drawback of creating true private method in javascript is that they are very memory inefficient because a new copy of the method would be created for each instance.

    Answered on February 21, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.