JavaScript EncapsulationThe JavaScript Encapsulation is a process of binding the data (i.e. variables) with the functions acting on that data. It allows us to control the data and validate it. To achieve an encapsulation in JavaScript: -
The encapsulation allows us to handle an object using the following properties: Read/Write - Here, we use setter methods to write the data and getter methods read that data. Read Only - In this case, we use getter methods only. Write Only - In this case, we use setter methods only. JavaScript Encapsulation ExampleLet's see a simple example of encapsulation that contains two data members with its setter and getter methods. Test it NowOutput: John 80 JavaScript Encapsulation Example: ValidateIn this example, we validate the marks of the student. Test it NowOutput: John undefined JavaScript Encapsulation Example: Prototype-based approachHere, we perform prototype-based encapsulation. Test it NowOutput: John 80
Next TopicJS Inheritance
|
JavaTpoint offers too many high quality services. Mail us on h[email protected], to get more information about given services.
JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected].
Duration: 1 week to 2 week