Forum Discussion

Preetha's avatar
Preetha
Occasional Contributor
6 years ago

How can I declare a static variable in java script

How can I declare a static variable in java script?

 

function CompareClassicCAT(img1,img2)
{
 var fileName, sheetName, colNumber,rowNumber, content;
fileName=Project.ConfigPath+"\AutoDataSource\\CATRegressionCompareTestResults.xlsx";
rowNo = 1;
colNo1=2;
colNo2=3;
sheetName="Test_Result";
if(Regions.Compare(img1,img2))
{

AddDataToExcelFile(fileName, sheetName, rowNo, colNo1, img1);
AddDataToExcelFile(fileName, sheetName, rowNo, colNo2, "Identical");
Log.Message(img1 + " is identical :)");
rowNumber++;
}
else
{
AddDataToExcelFile(fileName, sheetName, rowNo, colNo1, img2);
AddDataToExcelFile(fileName, sheetName, rowNo, colNo2, "NOT Identical");
Log.Message(img1 + " is NOT identical :)");
rowNumber++;
}
}

 

 

calling this function and the rowNumber , colNo1,colNo2 should not reinitialise.

  • I got the solution.. 

    initialise the variable outside the function as functionname.variable=value;

     

     

    CompareClassicCAT.rowNo = 1; //static variable
    CompareClassicCAT.colNo1= 2; //static variable
    CompareClassicCAT.colNo2= 3; //static variable


    function CompareClassicCAT(img1,img2)
    {

    var fileName,rowNo,colNo1,colNo2, sheetName, content;
    fileName=Project.ConfigPath+"\AutoDataSource\\CATRegressionCompareTestResults.xls";

    sheetName="Test_Result";

    if(Regions.Compare(img1,img2))
    {
    CompareClassicCAT.rowNo++;
    AddDataToExcelFile(fileName, sheetName, CompareClassicCAT.rowNo, CompareClassicCAT.colNo1, img1);
    AddDataToExcelFile(fileName, sheetName, CompareClassicCAT.rowNo, CompareClassicCAT.colNo2, "Identical");
    Log.Message(img1 + " is identical");

    }
    else
    {
    CompareClassicCAT.rowNo++;
    AddDataToExcelFile(fileName, sheetName, CompareClassicCAT.rowNo, CompareClassicCAT.colNo1, img2);
    AddDataToExcelFile(fileName, sheetName, CompareClassicCAT.rowNo, CompareClassicCAT.colNo2, "NOT Identical");
    Log.Message(img1 + " is NOT identical");

    }
    }

    • Preetha's avatar
      Preetha
      Occasional Contributor

      Ok But where to initialise the variable as i need to initilise it inside my function and when I call that function again it is reinitilising.

      • Preetha's avatar
        Preetha
        Occasional Contributor

        I got the solution.. 

        initialise the variable outside the function as functionname.variable=value;

         

         

        CompareClassicCAT.rowNo = 1; //static variable
        CompareClassicCAT.colNo1= 2; //static variable
        CompareClassicCAT.colNo2= 3; //static variable


        function CompareClassicCAT(img1,img2)
        {

        var fileName,rowNo,colNo1,colNo2, sheetName, content;
        fileName=Project.ConfigPath+"\AutoDataSource\\CATRegressionCompareTestResults.xls";

        sheetName="Test_Result";

        if(Regions.Compare(img1,img2))
        {
        CompareClassicCAT.rowNo++;
        AddDataToExcelFile(fileName, sheetName, CompareClassicCAT.rowNo, CompareClassicCAT.colNo1, img1);
        AddDataToExcelFile(fileName, sheetName, CompareClassicCAT.rowNo, CompareClassicCAT.colNo2, "Identical");
        Log.Message(img1 + " is identical");

        }
        else
        {
        CompareClassicCAT.rowNo++;
        AddDataToExcelFile(fileName, sheetName, CompareClassicCAT.rowNo, CompareClassicCAT.colNo1, img2);
        AddDataToExcelFile(fileName, sheetName, CompareClassicCAT.rowNo, CompareClassicCAT.colNo2, "NOT Identical");
        Log.Message(img1 + " is NOT identical");

        }
        }