Ask a Question

How to create user defined constants in JScript?

pvidal
Contributor

How to create user defined constants in JScript?

Hello,



I wanted to know how to create user defined constants in JScript. The help in the Code Metrics sugests to use constants for magic numbers, etc. Are these "constants" just variables defined at the beginning of the script? Or there is some king of special "const" keyword?



Thank you,

Patricio.
5 REPLIES 5
AlexanderM
Staff


Hi





I suggest that you see the "Declaring JScript Variables and Constants" and "User Defined Data Types" MSDN articles.
-----

Alexander
Customer Care Manager
pvidal
Contributor

Hello Alex,



Yes, I found those links before. The only form that does not gives a syntax error in TestComplete is "var <name> = <value>;". All the rest gives you a syntaxt error while processing "const":



    const daysInWeek  = 7;

    const favoriteDay  = "Friday";

    const daysInWeek : int = 7;



Regards,

Patricio.


Hi Patricio,





Sorry for the confusion. The articles I gave you are regarding JScript .NET. The scripting language used in TestComplete is not a .NET version. In regular JScript, constants are not supported, so the "const" statement is not available.





I suggest that you use variables instead. Please see the "About Variables" and the "Code Metrics" articles for the information.
--
Dmitry Nikolaev

Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️
asdfasdfasdfasd
Contributor

To minimize your global namespace, I suggest you create constants under an object literal:



var Constants = {

  A : 1,

  B : 2,

  C : 3

};
pvidal
Contributor

Good idea. Thanks!

Patricio.
cancel
Showing results for 
Search instead for 
Did you mean: