advertisement
Login | Register   
  Include Code  Search Tips
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
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
advertisement
Average Rating: 3.1/5 | Rate this item | 14 users have rated this item.
Encapsulate Your JavaScript: Keep Private Methods Private (cont'd)
A Test Run
Open a new document in your text editor, add the following code, and save it as oopTest.htm in the same folder as myObject.js:

 <html>
<head>
<title>OOP JavaScript Test</title>
<script language="JavaScript"
src="myObject.js"></script>
<script language="JavaScript">
var myLocalObject;
function initialize(){
myLocalObject = new myObject
(document.forms[0].prop1.value,
document.forms[0].prop2.value);
alert(myLocalObject.getProps());
}
function changeProps(){
myLocalObject.setProp1
(document.forms[0].prop1Change.value);
myLocalObject.setProp2
(document.forms[0].prop2Change.value);
alert(myLocalObject.getProps());
}
function testPM(){
myLocalObject.testPrivateMethod();
alert(myLocalObject.getProps());
}
</script>
</head>
<body>
<form method="POST">
<p>Property 1:
<input type="text" name="prop1" size="20"></p>
<p>Property 2:
<input type="text" name="prop2" size="20"></p>
<p><input type="button" value="Initialize"
name="init" onMouseDown="initialize();"></p>
<p>Change Property 1 to:
<input type="text" name="prop1Change"
size="20"></p>
<p>Change Property 2 to:
<input type="text" name="prop2Change"
size="20"></p>
<p>
<input type="button" value="Change Props"
name="change" onMouseDown="changeProps();">
</p>
<p>
<input type="button" value="Test Private Method"
name="test" onMouseDown="testPM();">
</p>
</form>
</body>
</html>
advertisement
Then open the file in a JavaScript 1.2 capable browser (I test with IE 5.0, Netscape 4.7, and Netscape 6.0). Start by entering values in the first two text fields and initialize the object. Then modify the values in the next two fields to change the properties. Finally, test the private method. You should see three alert boxes. The first two alerts display the values you entered. The third alert displays the predefined properties set in the private method.

As you can see, JavaScript can be more object-oriented than most people think. The ECMAScript specification, to which JavaScript must adhere, lists future reserved words such as class, super, import, and extends. These reserved words are all used by Java for class-based inheritance. This indicates to me that JavaScript will migrate even closer to Java in the not-too-distant future.

The chief benefit of using object-oriented principles is code reuse. In fact, the encapsulation process detailed above closely mirrors Java's component architecture—the JavaBeans specification. By encapsulating the functionality of an object, you or any other developer can simply forget about the implementation and make use of the functionality in any future project.
Previous Page: Build the Example  


Tom Duffy , DevX's JavaScript Pro, is an Associate Professor at Norwalk Community College, Norwalk, CT where he teaches Web Development and Java. Tom is also the Manager of the NCC Ventures Lab, the College's in-house web design studio.
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