|
Page 2 of 3
Create New Class Instance
Once your new class is created, you use it just as you'd use a C# class. For example, this is how you'd create a new class instance, and call its DisplayGreeting() method:
// create class instance var myHello = new HelloWorld(); // call method myHello.DisplayGreeting();
%2F%2F%20create%20class%20instance%20%0A%0D%0Avar%20myHello%20%3D%20new%20HelloWorld%28%29%3B%20%0A%0D%0A%2F%2F%20call%20method%20%0A%0D%0AmyHello.DisplayGreeting%28%29%3B
A possible implementation of the HelloWorld class is the following:
// "Hello, World" class function HelloWorld(hour) { // class "constructor" initializes this.hour field if (hour) { // if the hour parameter has a value, store it as a class field this.hour = hour; } else { // if the hour parameter doesn't exist, save the current hour var date = new Date(); this.hour = date.getHours(); } // display greeting this.DisplayGreeting = function() { if (this.hour >= 22 || this.hour <= 5) document.write("Goodnight, world!"); else document.write("Hello, world!"); } }
%2F%2F%20%22Hello%2C%20World%22%20class%20%0A%0D%0Afunction%20HelloWorld%28hour%29%20%0A%0D%0A%7B%20%0A%0D%0A%2F%2F%20class%20%22constructor%22%20initializes%20this.hour%20field%20%0A%0D%0Aif%20%28hour%29%20%0A%0D%0A%7B%20%0A%0D%0A%2F%2F%20if%20the%20hour%20parameter%20has%20a%20value%2C%20store%20it%20as%20a%20class%20field%20%0A%0D%0Athis.hour%20%3D%20hour%3B%20%0A%0D%0A%7D%20%0A%0D%0Aelse%20%0A%0D%0A%7B%20%0A%0D%0A%2F%2F%20if%20the%20hour%20parameter%20doesn%27t%20exist%2C%20save%20the%20current%20hour%20%0A%0D%0Avar%20date%20%3D%20new%20Date%28%29%3B%20%0A%0D%0Athis.hour%20%3D%20date.getHours%28%29%3B%20%0A%0D%0A%7D%20%0A%0D%0A%2F%2F%20display%20greeting%20%0A%0D%0Athis.DisplayGreeting%20%3D%20function%28%29%20%0A%0D%0A%7B%20%0A%0D%0Aif%20%28this.hour%20%3E%3D%2022%20%7C%7C%20this.hour%20%3C%3D%205%29%20%0A%0D%0Adocument.write%28%22Goodnight%2C%20world%21%22%29%3B%20%0A%0D%0Aelse%20%0A%0D%0Adocument.write%28%22Hello%2C%20world%21%22%29%3B%20%0A%0D%0A%7D%20%0A%0D%0A%7D
This code can be tested online at http://www.cristiandarie.ro/asp-ajax/ JavaScriptClass.html. The HelloWorld class is formed of the constructor code that initializes the hour field (this.hour), and of the DisplayGreeting() method—this.DisplayGreeting(). Fans of the ternary operator can rewrite the constructor using this shorter form, which also makes use of the object detection feature that was discussed in Chapter 2:
// define and initialize this.hour this.hour = (hour) ? hour : (new Date()).getHours();
%2F%2F%20define%20and%20initialize%20this.hour%20%0A%0D%0Athis.hour%20%3D%20%28hour%29%20%3F%20hour%20%3A%20%28new%20Date%28%29%29.getHours%28%29%3B
NOTE: The ternary operator is supported both by C# and JavaScript. It has the form (condition ? valueA : valueB). If the condition is true, the expression returns valueA, otherwise it returns valueB. In the shown example, object detection is used to test if a value was supplied for the hour parameter. If it was not, the current hour is used instead.
|