4.7. ConstructorsThe capability to create custom objects is what separates modern programming languages from the programming languages of yore. Unfortunately, in JavaScript, this capability is one of those language features that is often either ignored or overlooked. Believe it or not, there is actually a good reason for this; it is all a matter of perception. You see, JavaScript is often viewed as a lightweight language or a kid's programming language, good only for tasks such as creating pop-ups or handling mouseover events. Although I believe that everybody is entitled to their opinion, I also believe that this opinion has kept web applications mired in their original unload/reload glory. For this reason, as well as the fact that I'm not terribly fond of writing hundreds or thousands of lines of custom code, I began to play around with JavaScript constructors. Yes, with some planning and design work in the beginning, it is very possible to free up some time for the occasional mad scientist project later. 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. With that explained, let's take a look at some of the coding details of creating JavaScript constructors. I've always been fond of palindromes (words, numbers, or sentences that are spelled the same forward and backward), so let's create a constructor something along those lines. Without further ado, here is an introduction to the two ways of coding class constructors in JavaScript. Yes, there are two different ways to code class constructors in JavaScript. The first, which is probably the easier of the two, involves creating a function and then creating an instance of that function using the new operator. Listing 4-11 shows an annotated example of using this method to create a constructor. Listing 4-11. An Annotated Example of Creating a Class Constructor
To instantiate (a fancy way to say "create an instance") this class, all that is necessary is to use the new operator in the following manner: 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: alert(myMonster.palendrome()); alert(myMonster.number()); However, there is another way to create a class constructor in JavaScript: use the prototype property. This is shown in Listing 4-12. Listing 4-12. Using the prototype Property to Create an sclass Constructor
|