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

Front End TechnologyJavascriptWeb Development

Using private methods has a simple underlying concept. In this situation, you can declare private methods or private properties, which are used to hide a class's internal functionality from those other classes, whenever you wish to keep something private, whether it be a method or a property. You can include private fields, static methods, instance methods, and getters and setters that are only accessible to you.

Truly private fields and methods are delivered through private class features, with the language maintaining that privacy rather than a custom. Benefits include preventing naming conflicts between class features and the other of the code base and enabling classes to have a very limited interface.

Private methods, like private fields, are identified by a leading # and are not accessible to users outside of their class. They come in handy when a class needs to perform an intricate internal function but you don't want any other code to be able to call it.

Other programming languages, such as C++, Java, etc., define private as anything that isn't accessible directly through objects and can't be shared with other classes. The same concept also holds true for JavaScript.

Therefore, we must first examine how JavaScript creates private methods. The four major keywords that we can use to create a private method in the class are.

  • var
  • let
  • const
  • # (hash)

Example 1

In this example let us understand the basic example of private methods in JavaScript.

<!DOCTYPE html> <html> <title>What is the drawback of creating true private methods in JavaScript - TutorialsPoint</title> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body style="text-align:center"> <script> // written the class as tutpoint let tutpoint = function (company, launched) { // public member this.motorCar = "Maruti Suzuki Alto"; // written code for private member let topic = "privateMethod"; let status = launched; // written code for private member function let publish = () => { document.write(company +'<br>'); return status; } // written code to call private member // written function inside the same class document.write(publish() +'<br>'); }; // tutpoint1 is object of tutpoint class let tutpoint1 = new tutpoint('Maruti Suzuki Baleno', 'Maruti Suzuki Brezza'); // written to call public member outside the class document.write(tutpoint1.motorCar); </script> </body> </html>

When writing a real private method in JavaScript, there are two important drawbacks.

  • Private method calls from outside the class are not allowed.

  • When multiple instances of an object of the same class are produced, there is memory inefficiency because each example would require a new copy of the method.

Example 2

In this example let us understand how an error is returned if a private member function is called outside of the class.

<!DOCTYPE html> <html> <title>What is the drawback of creating true private methods in JavaScript - TutorialsPoint</title> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body style="text-align:center"> <script> let tutpoint = function (company, launched) { this.motorCar = "Maruti Suzuki Alto"; let topic = "privateMethod"; let status = launched; let publish = () => { document.write(company); return status; } document.write(topic +'<br>'); }; let tutpoint1 = new tutpoint('Maruti Suzuki Baleno', 'Maruti Suzuki Brezza'); document.write(tutpoint1.motorCar ); document.write(publish()); </script> </body> </html>

Please press the f12 key on your keyboard to access the browser console to see the results.

ReferenceError: publish is not defined

Example 3

In this example let us understand, if a private member function is called outside of the class. Therefore, the output from the example below is correct

<!DOCTYPE html> <html> <title>What is the drawback of creating true private methods in JavaScript - TutorialsPoint</title> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body style="text-align:center"> <script> class tutpoint { // Private field #motorCar; constructor(motorCar) { this.#motorCar = "Maruti Suzuki Alto"; } #privateFunction(prefix) { return prefix + this.#motorCar; } publicFunction() { return this.#privateFunction(">>"); } } let tutpoint1 = new tutpoint(); document.write(tutpoint1.publicFunction()); </script> </body> </html>

Example 4

In this example let us understand, If we construct multiple objects of the same class, since each object makes a duplicate of the function for itself, or its own instance, in that case. Then a memory efficiency issue occurs in that situation.

<!DOCTYPE html> <html> <title>What is the drawback of creating true private methods in JavaScript - TutorialsPoint</title> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body style="text-align:center"> <script> let motorCar = function (name, comp, cost) { this.name = name; this.company = comp; this.price = cost; // Private method let PriceIncrease = () => { this.price = this.price + 30000 +'<br>'; }; // Public method this.showPrice = () => { PriceIncrease(); document.write(this.price); }; }; const one = new motorCar("one", "Alto", 480000); const two = new motorCar("two", "Baleno", 1100000); const three = new motorCar("three", "Brezza", 1600000); one.showPrice() two.showPrice() three.showPrice() </script> </body> </html>
raja
Updated on 24-Aug-2022 07:09:29

Advertisements