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.
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 architecturethe 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.