advertisement
Premier Club Log In/Registration
  Include Code  Search Tips
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   SKILLBUILDING  |   TIP BANK  |   SOURCEBANK  |   FORUMS  |   NEWSLETTERS
Browse DevX
Download the code for this article
Cross-Browser Issues with the Sample Code
Are you currently using JavaScript's OOP capabilities? Are you aware that you can encapsulate access to properties and methods? Can you think of ways to improve the article code? Join the discussions at web.dhtml.general and web.dhtml.scripting to get answers, make comments, or help others with their problems.
Partners & Affiliates
advertisement
advertisement
advertisement
Average Rating: 2.8/5 | Rate this item | 10 users have rated this item.
Encapsulate Your JavaScript: Keep Private Methods Private (cont'd)
Build the Example
Open your favorite text editor, enter or download the code in Listing 1, and save the file as myObject.js:
advertisement


This code looks slightly different than most JavaScript. Notice that all the code exists within the function definition—in other words, the member functions are nested within the myObject() function. The myObject() function is the constructor for objects of type myObject. Nothing new there. The next two lines declare two member variables, prop1 and prop2, and initialize them. Notice that the function omits the this keyword and uses the var keyword instead. Omitting the this keyword makes the variables private to the object. The var keyword makes the property local (each object instance gets its own copy of the variable). Calls to internal methods then assign the passed argument values to the properties (see the SideBar Cross-Browser Issues with the Sample Code).

The next part of the code is a list of method pointers defined with the this keyword. The method pointers make the methods public. Each method pointer points to a getter or setter method that lets you retrieve and set the values of the private data member values. In essence, they let the object control how the values change internally. Although most of the methods shown only manipulate properties, you're can include non-property methods. For example, the getProps() method returns the list of property getters and setters.. Note that any method included inside the constructor that does not possess a method pointer is a private method. A private method can be used only by the object itself—it cannot be called from outside the object.

After declaring the method pointers, the myObject() function defines a set of nested functions. Only instances of myObject will have access to these private functions. Consider the following:

   var myObj = new myObject("Tom","Duffy");
   alert(getProp1());
The second line throws an error indicating that getprop1() is undefined. The correct call looks something like this:

   alert(myObj.getprop1());
That code will display "Tom"—as it should.

Finally, note that the private function called privateMethod() is invisible outside of the myObject function. By omitting the method pointer to privateMethod() and nesting it within the myObject() function, neither a calling script nor an object instance can make use of privateMethod(). It exists solely to provide some mechanism to another public method inside the object—in this case the trivial testPrivateMethod() function.
Previous Page: Encapsulation Next Page: A Test Run


Page 1: IntroductionPage 4: Encapsulation
Page 2: Is JavaScript Object-Oriented?Page 5: Build the Example
Page 3: Building JavaScript ObjectsPage 6: A Test Run
Please rate this item (5=best)
 1  2  3  4  5
advertisement
Advertising Info  |   Member Services  |   Permissions  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs