The first question is, how do we start writing a constructor? Do we just jump in and create a constructor and use it? Or should we work out the details of how something works and then use that to write a constructor? Which approach is better?
Tough questions, and, unfortunately, I can't say what will work for you. I can, however, tell you what works for me. Whenever I'm developing a constructor, the first thing that I do is write a sample application that does what I want it to do, but not using a constructor. After the sample application is developed the next step is to rewrite it using a constructor. This might seem like more work than it's worth, but it works for me. Also, I have a tendency to see a better way to accomplish tasks with each subsequent rewrite.
Listing 4-11. An Annotated Example of Creating a Class Constructor
var myMonster = new Monster();
Using the newly instantiated class is just a matter of using the various public properties and methods that were defined by the constructor. For example, to set and get the string property for the myMonster instance of the Monster class, the code would look like this:
myMonster.string = 'Able was I ere I saw Elba!'; alert(myMonster.string);
To use the properties methods, statements would look like the following:
Listing 4-12. Using the prototype Property to Create an sclass Constructor