在 JavaScript 中创建真正的私有方法有什么缺点?
使用私有方法有一个简单的底层概念。在这种情况下,您可以声明私有方法或私有属性,这些方法或属性用于隐藏类的内部功能,使其不被其他类访问,无论您何时希望保持某些内容私有,无论是方法还是属性。您可以包含私有字段、静态方法、实例方法以及仅供您访问的 getter 和 setter。
真正的私有字段和方法是通过私有类特性提供的,由语言维护该隐私,而不是自定义。好处包括防止类特性与代码库其他部分之间的命名冲突,并使类能够拥有非常有限的接口。
私有方法与私有字段一样,以 # 开头,并且无法被类外部的用户访问。当类需要执行复杂的内部函数但您不希望任何其他代码能够调用它时,它们会派上用场。
其他编程语言,例如 C++、Java 等,将私有定义为任何不能通过对象直接访问且不能与其他类共享的内容。同样的概念也适用于 JavaScript。
因此,我们必须首先检查 JavaScript 如何创建私有方法。我们可以使用四个主要的关键字在类中创建私有方法。
- var
- let
- const
- # (哈希)
示例 1
在这个例子中,让我们了解 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>
在 JavaScript 中编写真正的私有方法时,有两个重要的缺点。
不允许从类外部调用私有方法。
当生成同一类的多个对象实例时,会存在内存效率低下问题,因为每个示例都需要方法的新副本。
示例 2
在这个例子中,让我们了解如果在类外部调用私有成员函数会返回错误。
<!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>
请按键盘上的 f12 键访问浏览器控制台以查看结果。
ReferenceError: publish is not defined
示例 3
在这个例子中,让我们了解,如果在类外部调用私有成员函数。因此,以下示例的输出是正确的
<!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>
示例 4
在这个例子中,让我们了解,如果我们构造同一类的多个对象,因为每个对象都会为自己或其自己的实例复制函数,在这种情况下。那么在这种情况下就会出现内存效率问题。
<!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>
广告