When you're working with class members, the most important aspect is to explicitly define all members. To allow external code to access the class members (properties and methods), the this keyword must be used. So if you define a function XYZ() within the class, it is available as a class method only if you add this code: this.XYZ = XYZ; Within this method, you can also access class properties using this; however, they have to be defined as well. For accessing class members both internally and externally, the dot (.) is used to separate instance name and member name. The following code implements a simple class. The only class method, Count() (internal name: count()), takes one parameter and then counts in the given language. Figure 6.1 shows the result. A Class with Members (members.html)
Figure 6.1. The "Universal Counter."Note Another way to create objects is to use the Object constructor, in the following fashion: var uc = new Object(); uc.copyright = "(C) 2006 JavaScript Phrasebook "; uc.printCopyright = function() { window.alert(this.copyright); };
|