Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


Encapsulate Your JavaScript: Keep Private Methods Private-4 : Page 4




Application Security Testing: An Integral Part of DevOps

The concept of encapsulation means that an object's data members should remain private to the object and manipulated through publicly exposed methods. In other words, programs should not have the ability to change the properties of objects directly. In traditional JavaScript, you are free to modify the values of an object's properties simply through assignment. For example, you can modify a person object's data fields directly, which breaks the concept of encapsulation:

tDuffy.firstName = "Kelli";

Encapsulation states that firstName should not be available directly for manipulation and should be manipulated through a public method:


Most JavaScripters find the creation of private data members (variables and methods) foreign; however, it's rather easy to implement. A typical Java class contains a constructor, private member variables, and methods, and so does a JavaScript object. You need two techniques to accomplish encapsulation in JavaScript: nested functions and omission of the this keyword in object constructors. Be aware, though, that nested functions are part of the JavaScript 1.2 release, so they won't be available in downlevel browsers.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.
Thanks for your registration, follow us on our social networks to keep up-to-date