Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

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


advertisement
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>

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.


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.
Comment and Contribute

 

 

 

 

 


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

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date