JavaScript does not natively support the concept of protected methods or properties, but you can use closures to emulate this behavior. Here's an example of how you can add a protected static method to a class in JavaScript:
index.tsx229 chars14 linesIn this example, we define a class MyClass with two static methods: publicMethod, which anyone can call, and #protectedMethod, which is marked as "private" using the new # syntax from ECMAScript 2022. Then, we add a third static method, callProtectedMethod, which can be called by anyone but can also call the private #protectedMethod method.
Note that this is not true encapsulation; technically, any code can still access the #protectedMethod method by using the Reflect API or other tricks. However, it does provide a way to mark certain methods as "off-limits" to most code that uses your class, which can be useful in some cases.
gistlibby LogSnag