Java Q&A
by Nadine McKenzie

Listing One
function MyClassName(parameterA, parameterB)
{
    // --- Object Properties ---
    propertyR = "propertyR is read only.";
        
    // --- Object Initialization ---
    propertyA = parameterA;
    propertyB = parameterB;
        
    // --- Method Pointers ---
    this.getPropertyA = _getPropertyA;  
    this.setPropertyA = _setPropertyA;
    this.getPropertyB = _getPropertyB;
    this.setPropertyB = _setPropertyB;
    this.getPropertyR = _getPropertyR;
    this.doSomeAction1 = _doSomeAction1;

    // --- Methods ---
    function _getPropertyA()
    {
        return propertyA;
    }
    function _setPropertyA(para)
    {
        propertyA = para;
    }
    function _getPropertyB()
    {
        return propertyB;
    }
    function _setPropertyB(para)
    {
        propertyB = para;
    }
    function _getPropertyR()
    {
        return propertyR;
    }
    function _doSomeAction1()
    {
        alert(aPrivateMethod() + " " + this.getPropertyR());
    }
    function aPrivateMethod()
    {
        return "aPrivateMethod() is a private method.";
    }
}


Listing Two
    <html>
    <head>
    <script src="myclassname.js" language="JavaScript"></script>
    <script language="JavaScript">
        // object declaration and instantiation
        var objMyClassName = null;      
        objMyClassName = new MyClassName("A", "B");
        
        // accessing the newly created objects methods
        alert(objMyClassName.getPropertyA());           // Displays "A"
        alert(objMyClassName.getPropertyB());           // Displays "B"
    
        objMyClassName.setPropertyA("Y");
        objMyClassName.setPropertyB("Z");

   
        alert(objMyClassName.getPropertyA());           // Displays "Y"
        alert(objMyClassName.getPropertyB());           // Displays "Z"

        // Displays aPrivateMethod() is a private method. propertyR is read only.   
        objMyClassName.doSomeAction1(); 
                
        // Displays Undefined - propertyB is private
        alert(objMyClassName.propertyB);

       // Fails, aPrivateMethod() is private.  Netscape quietly bombs. 
       alert(objMyClassName.aPrivateMethod());         
 
    </script>
    </head>
    <body>
        <!-- body tags here -->
    </body>
    </html>



2

