Main Page

Using the TemperatureService object

This line of code creates a property called
webServiceSend
that points to the
send()
function. This
procedure makes it possible to redefine
send()
without losing the original functionality:
TemperatureService.prototype.send = function (sZipcode) {
this.zipcode = sZipcode;
return this.webServiceSend();
};
The first thing the new
send()
method does is assign the
zipcode
property. Then, it returns the
result of the
webServiceSend()
method (which is a call to the original
send()
method). The
TemperatureService
object can be used like this:
var oService = new TemperatureService();
var fResult = oService.send(“90210”);
alert(“Temperature is “ + fResult + “degrees”);
Using the TemperatureService object
To recreate the temperature example one more time, the code is much simpler:
<html>
<head>
<title>Cross-Browser Web Service Example</title>
<script type=”text/javascript” src=”detect.js”></script>
<script type=”text/javascript” src=”stringbuffer.js”></script>
<script type=”text/javascript” src=”http.js”></script>
<script type=”text/javascript” src=”webservice.js”></script>
<script>
function callWebService() {
var sZip = document.getElementById(“txtZip”).value;
var oService = new TemperatureService();
var fResult = oService.send(sZip);
alert(“It is currently “ + fResult + “ degrees in that zip code.”);
}
</script>
</head>
<body>
<p><input type=”text” id=”txtZip” size=”10” />
<input type=”button” value=”Get Temperature” onclick=”callWebService()” />
</p>
</body>
</html>
As you can see, the
callWebService()
function is greatly simplified in this example. The zip code is
obtained from the text box; then the
TemperatureService
object is created, and the zip code is passed
into the
send()
method, which returns the temperature in degrees. An alert is then displayed indicating
the temperature.
529
Web Services
20_579088 ch17.qxd 3/28/05 11:42 AM Page 529


JavaScript EditorFree JavaScript Editor     Ajax Editor


©