OOP JavaScript: Public and Private Methods

Through this simple JavaScript tutorial, you would learn about Public and Private methods in the JavaScript OOP.


Sampled by © JavaScriptBank.com

At Ning, here's how we do public and private methods in JavaScript:

/**
* A scrollable list that can display large numbers of contacts.
*
* @param contacts contact objects
*/
xp.ContactSelector = function(args) {

/** Container for public functions. */
var self = {};

/** Container for private functions. */
var _ = {};

/**
* Initializes the object.
*/
_.initialize = function() {
if (!args.contacts) {
_.installSearchbox();
}
};

/**
* Adds a searchbox to the ContactSelector.
*/
_.installSearchbox = function() {
// This is a private method
};

/**
* Returns the contacts that the user has selected
*
* @return the selected contact objects
*/
self.getSelectedContacts = function() {
// This is a public method
};

. . . . . . . . . .

_.initialize();
return self;
};


Private methods begin with _., while public methods begin with self.. There are some interesting things about this setup:
  • Public methods are accessible on the object created by this function. Private methods are not accessible.
  • Public and private methods can be defined in any order, regardless of which calls which. This is because they are properties of self and _. If they were defined as standalone functions, then order would matter.
  • You can inherit from another object by adding the following to initialize(): self = xp.AbstractContactSelector(args);

Language
Translate this page to English Translate this page to French Translate this page to Vietnamese

Recent articles
Insights for Advanced Zooming and Panning in JavaScript Charts
How to open a car sharing service
Vue developer as a vital part of every software team
Vue.js developers: hire them, use them and get ahead of the competition
3 Reasons Why Java is so Popular
Migrate to Angular: why and how you should do it
The Possible Working Methods of Python Ideology
JavaScript Research Paper: 6 Writing Tips to Craft a Masterpiece
Learning How to Make Use of New Marketing Trends
5 Important Elements of an E-commerce Website


Top view articles
Adding JavaScript to WordPress Effectively with JavaScript Localization feature
Top 10 Beautiful Christmas Countdown Timers
Top 10 Best JavaScript eBooks that Beginners should Learn
65 Free JavaScript Photo Gallery Solutions
16 Free Code Syntax Highlighters by Javascript For Better Programming
Best Free Linux Web Programming Editors
Top 50 Most Addictive and Popular Facebook mini games
More 30 Excellent JavaScript/AJAX based Photo Galleries to Boost your Sites
Top 10 Free Web Chat box Plug-ins and Add-ons
The Ultimate JavaScript Tutorial in Web Design


Free JavaScript Tutorials & Articles
at www.JavaScriptBank.com