faqts : Computers : Programming : Languages : JavaScript : Language Core : Objects

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

8 of 25 people (32%) answered Yes
Recently 4 of 10 people (40%) answered Yes

Entry

JavaScript: Class: Create: Mile: Kilometer: Convert: How to create class convert mile to kilometer?

Apr 15th, 2006 11:46
Knud van Eeden,


----------------------------------------------------------------------
--- Knud van Eeden --- 16 January 2005 - 03:20 am --------------------

JavaScript: Class: Create: Mile: Kilometer: Convert: How to create 
class convert mile to kilometer?

---

Steps: Overview:

 1. -Save first listing as 'MathConvertUnitKilometersMilesC.js'

--- cut here: begin --------------------------------------------------

function MathConvertUnitKilometersMilesC() {
 //
 // --- Method Pointers: public methods --- //
 //
 // this.<your public function> = <your private nested function>
 //
 this.FNMathGetConversionUnitMilesToKilometerD = 
FN_MathGetConversionUnitMilesToKilometerD;
 this.FNMathGetConversionUnitKilometerToMilesD = 
FN_MathGetConversionUnitKilometerToMilesD;
 //
 // --- Methods: Private: do something --- //
 //
 function FN_MathGetConversionUnitMilesToKilometerD( milesD ) {
  return( 1.609 * milesD );
 }
 //
 function FN_MathGetConversionUnitKilometerToMilesD( kilometersD ) {
  return( kilometersD / 1.609 );
 }
}

--- cut here: end ----------------------------------------------------

 2. -Save second listing as 'MathConvertUnitKilometersMilesC.html'
     in the same directory

--- cut here: begin --------------------------------------------------

<HTML>
  <!--- --->
  <HEAD>
  <!--- --->
  <SCRIPT
    LANGUAGE="JavaScript"
    SRC="MathConvertUnitKilometersMilesC.js">
  </SCRIPT>
  <!--- --->
  <SCRIPT
    LANGUAGE=
     "JavaScript"
   >
    //
    // object declaration and instantiation
    //
    var myobjectnameO = null;
    myobjectnameO = new MathConvertUnitKilometersMilesC();
    //
    // accessing the newly created object methods
    //
    alert( myobjectnameO.FNMathGetConversionUnitMilesToKilometerD( 
1.0 ) ); // displays "1.609"
    //
    alert( myobjectnameO.FNMathGetConversionUnitKilometerToMilesD( 
1.0 ) ); // displays "0.621504039776259"
    //
  </SCRIPT>
  <!--- --->
  </HEAD>
  <!--- --->
  <BODY>
    <!-- your body tags here -->
  </BODY>
  <!--- --->
</HTML>

--- cut here: end ----------------------------------------------------

 3. -Open your browser

     1. Load the file 'MathConvertUnitKilometersMilesC.html'

        1. -Note: you will have to adapt the file path to the
                  conditions on your computer

 4. -Run the program

 5. -That will show a screen output similar to the following:

--- cut here: begin --------------------------------------------------

+-------------------------+
|                         |
|         1.609           |
|                         |
|                         |
|         [OK]            |
+-------------------------+


+-------------------------+
|                         |
|    0.621504039776259    |
|                         |
|                         |
|         [OK]            |
+-------------------------+


--- cut here: end ----------------------------------------------------

===

Tested successfully on
Microsoft Windows XP Professional (service pack 2),
running
Microsoft Internet Explorer v6.x

===

Internet: see also:

---

JavaScript: Object oriented: Class: Link: Overview: Can you give an 
overview of links?
http://www.faqts.com/knowledge_base/view.phtml/aid/33407/fid/307

---

Class: Create: Unit: Mile: Kilometer: Convert: Link: Overview: Can you 
give an overview of links?
http://www.faqts.com/knowledge_base/view.phtml/aid/33411/fid/138

----------------------------------------------------------------------