13.1.1 run JavaScript
We have already introduced that JavaScript is usually embedded in an interconnected web page for execution. There are two ways to embed and execute JavaScript code in an HTML page.
Use javascript: prefix to construct the URL for executing JavaScript code.
Use the <SCRIPT> element to include JavaScript code.
Code example:
// Method 1
<A href = "javascript: Alert ('run JavaScript! '); "> Run javascript <A/>
// Method 2
<SCRIPT type = "text/JavaScript">
Alert ("directly running JavaScript! ");
</SCRIPT>
13.1.2 import JavaScript files
To better separate HTML pages from JavaScript scripts, we can save the JavaScript scripts separately in a *. js file.
Code example:
<SCRIPT src = "test. js" type = "text/JavaScript"> </SCRIPT>
In the above syntax, the src attribute specifies the URL of the Javascript script file. The script file separation method is consistent with the script written directly on the page.
13.2 data types and variables
Data Types and variables are indispensable for any language. The following describes the basic syntax of data types and variables in JavaScript.
13.2.1 how to define variables
Javascript is a weak scripting language. You do not need to define a variable before using it. You can use it directly when you want to use a variable. To sum up,
Javascript supports two methods to introduce variables.
Implicit definition: assign values directly to variables.
Display definition: Use the VaR keyword to define variables.
Code example:
// Define variable A in implicit Mode
A = "Hello JavaScript ";
Alert ();
// Display mode definition variable
VaR A = "Hello JavaScript ";
Alert ();
The implicit definition method is simple and convenient. You can directly assign values to variables when you need to use variables.
The declaration mode uses the VaR keyword to declare a variable. The variable can have no initial value when declared, and the declared variable data type is uncertain. When the variable is assigned for the first time
The value assignment determines the Data Type of the variable, and the Data Type of the variable can be changed at will during use.
13.2.2 type conversion
Javascript supports automatic type conversion, which is very powerful.
Code example:
// Define string variables
VaR A = "3.145 ";
// Perform arithmetic operations on string variables and values
VaR B = A-2;
// Perform addition operations on string variables and values
VaR c = a + 2;
In the code above, A is a string with a value of 3.145. If a and the value are subtracted, the arithmetic operation is automatically executed and the type is converted to a value;
If a and numeric values are added, the value of a is converted to a string. This is automatic type conversion. The conversion rules are as follows:
For the minus sign operator, because the string does not support subtraction, the system automatically converts the string to a value.
For the plus sign operation, because the plus sign can be used as the join operator for a string, the system automatically converts various types of values.
The results of automatic conversion of various types are shown in the following table:
Results of automatic conversion of various types
Value |
Target type |
String type |
Numeric type |
Boolean |
Object |
Undefined |
"Undefined" |
Nan |
False |
Error |
Null |
"Null" |
0 |
False |
Error |
String |
Unchanged |
Numeric value or Nan |
True |
String object |
Null String |
Unchanged |
0 |
False |
String object |
0 |
"0" |
0 |
False |
Number object |
Nan |
"Nan" |
Nan |
False |
Number object |
Infinity |
"Infinity" |
Infinity |
True |
Number object |
-Infinity |
"-Infinity" |
-Infinity |
True |
Number object |
Value |
Numeric string |
Unchanged |
True |
Number object |
True |
"True" |
1 |
Unchanged |
Boolean object |
False |
"False" |
0 |
Unchanged |
Boolean object |
Object |
"Tostring () Return Value" |
Valueof (), tostring () or Nan |
True |
Unchanged |
This automatic type conversion is convenient, but the program is quite readable, and sometimes we want to perform addition operations on strings and values, which requires forced type conversion.
Javascript provides the following functions for forced type conversion.
Tostring (): converts a Boolean value or a value to a string.
Parseint (): converts string and boolean values to integers.
Parsefloat (): converts string and boolean values to floating point numbers.
13.2.3 variable
Javascript is a weak type language. The same variable can store values and strings in a short time. Variables also have an important concept: scope of action.
Variables are classified into global variables and local variables based on their defined ranges. Global variables can be used by all scripts.
The variable defined in the function is called a local variable, which is only valid in the function. If the global and local variables use the same variable name
Local variables overwrite global variables.
Code example:
// Define the global variable test
VaR test = "global variables ";
// Define the function checkscope
Function checkscope ()
{
// Define local variables
VaR test = "local variable ";
Alert (test );
}
Checkscope ();
The execution result will be a "local variable". The Code defines a global variable named test, but the function defines a local variable named test.
The local variable of overwrites the global variable. What is different from Java and C languages is that JavaScript variables have no block scope.
Code example:
Function Test (o)
{
If (typeof o = "object ")
{
// Define the variable J. The scope of the variable J is within the entire function, rather than within the if block.
VaR J = 5;
For (var k = 0; k <10; k ++)
{
// Because JavaScript has no code block range
// Therefore, the scope of K is within the entire function, rather than circulating the body.
Document. Write (k );
}
}
// Even if the cyclic body exists, the value of K still exists.
Alert (K + "\ n" + J );
}
July 26 = 276-285