SoFunction
Updated on 2025-04-14

Wireless Markup Language (WML) Basics Page 2/2


4.3.3 Basic writing rules for WMLScript programs
Basic book writing rules for WMLScript programs:
1. A program is composed of several statements or functions, and a function is composed of several statements;
2. Each complete statement must be followed by a semicolon (;), and there must be space between the operands of the statement keywords; semicolon (;) is a component of the WMLScript program;
3. The function body must be enclosed with pairs of curly braces ({ }), and at the end of the function, semicolons (;); the function description part, such as function name, function type, function parameters, etc., must be placed in front of the curly braces ({});
4. Some statements may also require practical curly braces ({ }) to contain content. Such statements can usually be placed in functions, so curly braces ({ }) can be nested.
Of course, different statements, parameters, variables and other elements may have some more detailed requirements when declaring and writing them. We will introduce these elements later to give them special.
4.4 Variables and data types
Variables, data types, are concepts and components of all programming languages, and WMLScript is no exception. It provides detailed regulations on its variable usage methods and data type definition methods. Variables usually correspond to certain data. We can assign values ​​to variables and change the value of variables during program execution. Below we explain the detailed provisions of WMLScript on variables and data types.
4.4.1 Variables and their declarations
A variable is a symbolic name, or identifier, that has a value in a WMLScript script program. Use variables to store and change the data required in the program. Unlike C, WMLScript only supports variables defined in functions or parameter variables used to transfer numbers.
A variable must be declared before use, that is, a variable is defined, that is, the name of the variable is specified. The keyword that declares a variable is var, which is the legitimate identifier of the variable name at the root after it, and a semicolon (;) is added at the end, which means that a quantity declaration is completed. Declaring a variable is that multiple variable names can be declared at once using var, and adjacent variables use the same number (,) interval.
Generally speaking, when we name variables, we hope to use meaningful variable names. For example, when you need to use a variable to represent the price of a book, although there is no error in naming the variable as j or book, if you can name it as bookPrice, it will be a WMLScript script program that has better readability and can facilitate programmers to write and debug scripts. Moreover, since WMLScript cannot use reserved words when naming variables, considering that variable names want to retain words conflict due to temporary negligence, we recommend using multiple words to combine them as a variable name, which is a better solution. For example, if you want to define a variable to store the price of a book, then we might as well use bookPrice or book_price as the name of the variable. This way, on the one hand, the variables can appear clearer, and on the other hand, the conflict between variables and reserved words can also be avoided.
The above is just our suggestions for variable naming, not a mandatory requirement. Users can do it without following our requirements. As long as they comply with the requirements of WMLScript for identifier naming, it is enough, but developing a good programming style is very meaningful for programmers and scriptwriters.
4.4.2 The scope and life period of variables
The scope of a variable refers to a piece of code that can reference this variable in the program. Since WMLScript only supports variables defined in functions, the function that usually defines them is the function that defines them. Outside this function, variables no longer play a direct role.
The start of life of a variable starts from the variable declaration until it expires. The life period of a variable is also called the duration and survival period of a variable. A variable is valid throughout the entire function that defines it, and no statement block within the function will reduce the lifetime of the variable or limit the scope of the variable.
If a variable is used directly without life, or is declared again after declaration, it will destroy the life period of the variable. The former situation will cause the variable to not start the declaration period, that is, there is no "life"; while the latter situation will cause the variable to re-assign the declaration period before the declaration period ends, that is, it will "born" many times. This will cause the variable to be invalid. The use of variables in the following functions illustrates this problem:
function foo(){
x=1;//Error: The variable is not declared before use, and the change variable has not been "declared".
var x,y,z;
y=x+3;
var zd =invalid
if(x){
var(y);//Error: This variable has been declared, here is a repeated declaration.
};
};
4.4.3 Use of variables
WMLScript variables can only be used within the functions that define it. When using it, you need to declare variables. Declaring variables can be assigned values ​​to variables at the same time, or even perform operations on variables. For example, the following simple function illustrates this flexible way to use variables:
function ourAge(){
var myAge=38;
var yourAge=26;
var ourAge=myAge+yourAge;
return ourAge;
};
When using variables, it can be achieved by calling the variable name. In the above example, the "var ourAge=myAge+yourAge;" sentence, by calling the variable name, the variable ourAge performs a sum operation on the variable myAge and the variable yourAge.
4.4.4 Variable types and data types
WMLScript is a "weak type" language, and its variables have no definite types. The type of WMLScript variable is determined by the type of data assigned by the variable and is changed according to the change of the data type. WMLScript only supports internally defined data, so we write programs without specifying the type of WMLScript variables. The WMLScript subtraction type assigned to the data according to the variables will automatically match. Since the data types of WMLScript include five types: integer, floating point, string, boolean and "invalid" types, these five types can match the types of WMLScript variables.
Previous page12Read the full text