how can we mimic access modifiers of java in javascript?
Access modifiers
Read this, it contains the detailed explanation of js classes, and how to make the private, public etc.
Hey Lavanya,
As you are not responding to this thread, I am marking your doubt as Resolved for now. Re-open it if required.
Please mark your doubts as resolved in your course’s “ Ask Doubt ” section, when your doubt is resolved.
JavaScript does not have direct equivalents to the access modifiers in Java, such as “public,” “private,” and “protected.” However, there are ways to achieve similar functionality in JavaScript:
- Closures: By creating closures, you can create private variables and methods that are only accessible within a specific scope.
- Object properties: You can use object properties to simulate the concept of private variables. You can set properties as non-enumerable or write-only, which makes them difficult to access from outside the object.
- WeakMap: You can use WeakMap to store private data associated with an object. The private data stored in a WeakMap cannot be accessed or enumerated.
- Symbol: You can use the Symbol type to create unique keys that can be used as private property names.
- Modules: You can use the module pattern to create private variables and methods that are only accessible within the scope of the module.