Five. Object Application Routines
Another example of a thermometer (thermometer) object, which is responsible for converting different temperature scales:
<script language= "JavaScript" >
Constructor
function thermometer (degrees, scale)
{
Methods
This.converttocelsius = Converttocelsius;
This.converttofahrenheit = Converttofahrenheit;
This.raisetemp = raisetemp;
Action to take
if (scale = = "F" | | scale = = "F")
{
This.scale = scale;
THIS.DEGREESF = degrees;
This.degreesc = 0;
This.converttocelsius ();
}
Else
{
This.scale = scale;
THIS.DEGREESF = 0;
This.degreesc = degrees;
This.converttofahrenheit ();
}
}
Conversion functions
function Converttocelsius ()
{
This.degreesc = (5.0/9.0) * (this.degreesf-32.0);
}
function Converttofahrenheit ()
{
THIS.DEGREESF = ((9.0/5.0) * this.degreesc) + 32.0;
}
method to raise temperature
function raisetemp (num)
{
THIS.DEGREESF + = num;
This.degreesc + = num;
}
</script>
The meaning of the above code is that the constructor creates an object, initializes it with a temperature and temperature scale, and then runs the transformation function to obtain the equivalent temperature under another scale. It contains a raisetemp () method to demonstrate how the object properties are modified.
The following code demonstrates how to use an object in an HTML document:
<script language= "JavaScript" >
Create an object instance
A = new thermometer (98.6, "F");
Access Object Properties
Alert ("Temperature in Fahrenheit" + A.DEGREESF);
Alert ("Temperature in Celsius" + A.degreesc);
Execute Object Methods
A.raisetemp (10);
Alert ("Temperature in Fahrenheit" + A.DEGREESF);
Alert ("Temperature in Celsius" + A.degreesc);
</script>
Six. Passing Object parameters
You can also pass an object to another object, just as you can pass an argument to an object. Take a look at the following example, which contains two object constructors, and the second is set to receive an object as a parameter:
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.