What is the drawback of creating true private methods in JavaScript ? Last Updated : 21 Mar, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report According to other programming languages like c++, java, etc. private is something that can't be shared with other classes, which cannot be accessed directly using objects. The same rule applies to JavaScript also. So first we have to look at how private methods are made using JavaScript? There are mainly four keywords using that we can make a private method in the class. varletconst# (hash) Example: JavaScript <script> // class Geek var Geek = function (article, published) { // public member this.programmingLang = "Js"; // private member var topic = "private_method"; var status = published; // private member function var publish = () => { console.log(article); return status; } // calling private member // function inside same class console.log(publish()); }; // Geek1 is object of Geek class var Geek1 = new Geek('ABC', 'YES'); // calling public member outside the class console.log(Geek1.programmingLang); <script/> Output: ABC YES Js There are two major disadvantages of creating the true private method in JavaScript. Cannot call private method outside the class.Create memory inefficiency when different objects are created for the same class, as a new copy of the method would be created for each instance. Example: If we call a private member function outside the class, then in that case it gives an error. JavaScript <script> var Geek = function (article, published) { this.programmingLang = "Js"; var topic = "private_method"; var status = published; var publish = () => { console.log(article); return status; } console.log(topic); }; var Geek1 = new Geek('ABC', 'YES'); console.log(Geek1.programmingLang); console.log(publish()); <script /> Output: But below example gives the correct output: JavaScript <script> class A { // Private field #foo; constructor(foo) { this.#foo = "ABC"; } #privateFun(prefix) { return prefix + this.#foo; } publicFun() { return this.#privateFun(">>"); } } let a = new A(); console.log(a.publicFun()); <script/> Output: >>ABC Example: If we create a different object for the same class, then in that case each object creates its own instance or say a copy of the function for itself. Then in that case memory inefficiency problem arises JavaScript <script> var Employee = function (name, comp, sal) { this.name = name; this.company = comp; this.salary = sal; // Private method var Sal_increase = () => { this.salary = this.salary + 45000; }; // Public method this.dispalySal = () => { Sal_increase(); console.log(this.salary); }; }; const A = new Employee("A", "dream", 45000); const B = new Employee("B", "young", 6000); const C = new Employee("C", "inspire", 9000); A.dispalySal() B.dispalySal() C.dispalySal() </script> Output: 90000 51000 54000 Note: The code here mentioned can be run using inside HTML or a console. Comment More infoAdvertise with us Next Article How to create a private variable in JavaScript ? S surbhikumaridav Follow Improve Article Tags : JavaScript Web Technologies Geeks Premier League Geeks-Premier-League-2022 JavaScript-Questions Similar Reads What is the practical use for a closure in JavaScript? In JavaScript, closures are defined as inner functions that have access to variables and parameters of outer function even after the outer function has returned. The below examples show the practical use of closures: Example: In this example, a variable mult is defined that is local to the function 2 min read JavaScript - What is the Purpose of Self Executing Function? The self-executing anonymous function is a special function which is invoked right after it is defined. There is no need to call this function anywhere in the script. This type of function has no name and hence it is called an anonymous function. The function has a trailing set of parentheses. The p 2 min read How to create a private variable in JavaScript ? In this article, we will try to understand how we could create private variables in JavaScript. Let us first understand what are the ways through which we may declare the variables generally in JavaScript. Syntax: By using the following syntaxes we could declare our variables in JavaScript. var vari 3 min read What Are Access Modifiers In JavaScript ? Access modifiers in JavaScript play a significant role in controlling the visibility and accessibility of class members. Although JavaScript is not traditionally an object-oriented programming (OOP) language, the introduction of classes and access modifiers in ECMAScript 6 (ES6) allowed developers t 5 min read How to make variables private in Constructor functions in JavaScript ? In Javascript there's no specific way to make variables private in a Constructor function. If a variable is genuinely private, meaning it cannot be accessible from the outside. A constructor is a function that generates a new instance of a class, sometimes referred to as an "object." A constructor's 2 min read What are the classes and proxies in JavaScript ? Classes: These are almost similar to functions, except they use a class keyword instead of a function keyword. Another important difference between functions and classes is that functions can be called in code before they are defined whereas classes must be defined before a class object is construct 4 min read Like