1. Strict Mode)
ECMAScript 5 introduces the Strict Mode concept.
JavaScript is a more rigorous parsing and execution mode. To use the strict mode for the entire script, add the following statement at the top of the script:
"Use strict ";
To enable a function to use the strict mode, use the following method:
Function doSomething () {"use strict"; // function body}
2. Declaration and scope of Variables
(1) Within a code block, variables declared using the var keyword are local variables, and the scope is only within the functions or objects that directly contain the code block; variables directly initialized without using the var keyword Declaration are global variables. It is best not to initialize the variable directly without declaring it first. (2) When multiple variables are declared using a var keyword and a comma, variables of the same type are placed in the same row, variables of different types are not placed in the same row, and indentation is used. Although this is not necessary, doing so can increase readability, for example:
Var message = "hi", name = "Jim Green", found = false, age = 29;
3. typeof Operator
(1) The typeof operator can return the Data Type of a variable. The returned result is one of the following: "undefined" (the value is undefined) "boolean" (the value is boolean) "string" (value is string type) "number" (value is numeric type) "object" (value is object type) "function" (value is function type) (2) example of using the typeof OPERATOR: var message = "some string"; alert (typeof message); // "string" alert (typeof (message )); // "string" alert (typeof 95); // "number" (3) typeof is an operator rather than a function. Parentheses are not required. The result returned by calling typeof null is "object", because null is considered to be a reference to an empty object. For regular expressions, typeof returns "object" in most browsers and "function" in Some browsers ".
4. Unde incluned classType
(1) The Unde incluned type has only one undefined value. If a variable declared using var is not initialized, its default value is undefined. Undefined can be explicitly assigned to a variable, but this is generally not recommended. (2) variables with undefined values are different from those with undefined values. The result of referencing a variable with a value of undefined is undefined. The result of referencing an undefined variable produces an error. However, if the typeof operator acts on both variables, the result will be undefined, which will produce ambiguity. Therefore, it is recommended that you always initialize a variable. The delete operator does not produce errors when acting on an undeclared variable, but errors are generated in strict mode.
5. NULL type
(1) The NULL type has only one null value. It is actually a null object pointer. (2) The value undefined is derived from null, so the ECMA-262 defines that the two are equal, as shown below:
Alert (null = undefined); // true
6. Boolean type
(1) The Boolean type has only two values: true and false, which are different from the numeric type. Therefore, true and 1 are not equal to other non-zero values, and false and 0 are not equal. This is similar to the Boolean Type of C. (2) All types of values have corresponding Boolean values. You can use the Boolean () Conversion Function to convert a value to the corresponding Boolean value. Whether the conversion result is true or false depends on the data type and its value. Various conversions are as follows:
If the data type conversion result is true, the conversion result is false ---------------------------------------------------------------------- Boolean true false String non-null String "" (empty String) Number any value 0, naN Object any Object null Undefined n/a undefined in the if statement, will automatically (implicitly) convert various types of values to Boolean type values, this is useful in function detection technology.
7. Number Type
(1) The numeric types in JavaScript can be expressed in decimal, hexadecimal, and octal notation, which is similar to other C-language programming languages. However, in the strict mode, errors are thrown. (2) For a large or small number, you can use an exponential representation. The base number of the index is E in upper or lower case. For example: var floatNum = 3.125e7; // equal to 31250000 (2) floating point operations cannot be expected for some values. For example, the result of adding 0.1 and 0.2 will be 0.30000000000000004 rather than 0.3.
8. Value Range
JavaScript Based on ECMAScript standards cannot represent all values. values with extremely large and extremely small absolute values cannot exceed a certain range. The minimum value is stored in the Number. MAX_VALUE attribute, Which is 5e-324 in most browsers; the maximum value is stored in the Number. MAX_VALUE attribute, and 1.7976931348623157e + 308 in most browsers. If a value is out of the range, Infinity is used. Positive values are Infinity, and negative values are-Infinity. Values of positive or negative Infinity cannot be involved in calculation. The isFinite () function can be used to determine whether a value is out of the range. If the returned value is true, the value does not exceed the range. If the returned value is false, the value exceeds the range. For example:
Var result = Number. MAX_VALUE + Number. MAX_VALUE; alert (isFinite (result); // false
The values-Infinity and infinity are stored in the Number. NEGATIVE_INFINITY attributes and Number. POSITIVE_INFINITY attributes respectively.
9. NaN
NaN (Not a Number for short) is a special value. If an operation fails to return a value, the result is NaN. For example, if any number is divided by 0, errors may occur in other languages, and the program stops executing. In JavaScript, the result is NaN, and the program can continue executing. The result of any NaN operation will also be NaN. NaN is not equal to or even not to NaN itself. The isNaN () function is used to determine whether a value is NaN. It accepts a parameter, which can be any data type. IsNaN () tries to convert the input parameter to the value type. If it can be converted to the value type, false is returned. If it cannot be converted to the value type, true is returned. For example:
Alert (isNaN (NaN); // true alert (isNaN (10); // false-10 is a value alert (isNaN ("10 ")); // false-values can be converted to 10 alert (isNaN ("blue"); // true-values cannot be converted to alert (isNaN (true )); // false-it can be converted to a value of 1.
10. Numeric conversion functions
(1) Number (arg): The arg parameter can be of any data type. for different data types, the conversion rules are more complex, the same as the + operator. For details, see this book. In many operators and operations, this function is implicitly called to convert values of other data types to values of the numerical type for operation or operation. For example:
Var num1 = Number ("Hello world !"); // NaN var num2 = Number (""); // 0 var num3 = Number ("000011"); // 11 var num4 = Number (true); // 1
(2) parseInt (arg): arg is a string type, for example:
ParseInt ("55"); // 55 parseInt ("55 66"); // 55 parseInt ("+"); // NaN parseInt ("-"); // NaN parseInt ("+ 5"); // 5 parseInt ("-5"); //-5 parseInt ("a5 "); // NaN parseInt ("0.5"); // 0 parseInt (". 5 "); // NaN parseInt (" 1234blue "); // 1234 parseInt (" "); // NaN parseInt (" 0xA "); // 10-hexadecimal parseInt (22.5); // 22 parseInt ("70"); // 70-decimal parseInt ("0xf"); // 15-hexadecimal
However, for a numeric string starting with 0 like "070", ECMAScript 3 will be parsed and converted to decimal by octal notation, and the result will be 56; in strict or non-strict mode, ECMAScript 5 returns 0, IE 9 returns 70, and FireFox 10 and Chrome 17 returns 56.
(3) parseInt (arg, radix): The arg parameter is a string. The radix parameter specifies the hexadecimal conversion method, but regardless of the hexadecimal conversion method, the final result is in decimal format. Example: parseInt ("0xAF", 16); // 175 parseInt ("AF", 16); // 175 parseInt ("AF "); // NaN parseInt ("10", 2); // 2-parseInt ("10", 8) as binary parsing ); // 8-Parse parseInt ("10", 10) as an octal parser; // 10-Parse parseInt ("10", 16) as a decimal parser ); // 16-as hexadecimal parsing (4) parseFloat (arg): The parameter arg is a string. Example: parseFloat ("1234blue"); // 1234-integerparseFloat ("0xA"); // 0 parseFloat ("22.5 "); // 22.5 parseFloat ("22.34.5"); // 22.34 parseFloat ("0908.5"); // 908.5 parseFloat ("3.125e7"); // 31250000
11. String
(1) The value of a string can be enclosed by double quotation marks or single quotation marks. There is no essential difference between the two. (2) The length attribute is used to obtain the number of characters contained in a string. The length of a string containing escape characters is determined by the escape result. (3) The string cannot be changed, for example:
Var lang = "Java"; lang = lang + "Script"; after the last statement is completed, lang will be a new variable, and "Java" and "Script" will be destroyed. These tasks are all done behind the scenes. (4) The toString () method is used to convert data to a string. This method is available for almost all data types and values. However, if the value is null or undefined, the method is unavailable. The toString (radix) method accepts a parameter used to output a numeric string in the specified hexadecimal format, for example, var num = 10; alert (num. toString (); // "10" alert (num. toString (2); // "1010" alert (num. toString (8); // "12" alert (num. toString (10); // "10" alert (num. toString (16); // "a" (5) String (arg) method is used to convert data into strings. If the data value has the toTring () method, it is called. If the value is null, the conversion result is "null". If the value is undefined, the conversion result is "undefined ". You can also use the + operator to add an empty string "" to the value to convert it to a string.
12. Operators
(1) ++ and -- operators can operate other data types in addition to numeric data. However, other data types are converted to corresponding numeric data types according to certain rules. (2) Bit operations are more efficient. Bitwise operators can directly manipulate numeric data. NaN and Infinity are processed as 0. Data of other data types will be converted to the value type for calculation, and the calculation result page will be the value type. A positive value of the value type is expressed in binary notation, and a negative value is expressed in binary complement notation. (3 )~ An operator is a complement operator in bitwise operations ,~ The result of num and-(num + 1) is equal. However, the former is more efficient. (4) | it is a binary logic or operator, and its result does not always return a Boolean value. If the first operand is of the object type, the first operand is returned. If the first operand is false, the second operand is returned. If the first operand is null, the second operand is returned; if both are null and NaN, the corresponding null or NaN is returned. If one of the variables is not declared, errors will occur. Due to the features of the | Operator, the following assignment statements are widely used:
// When preferredObject is not null, the result of myObject is // preferredObject. If preferredObject is null, The result // is backupObject. Var myObject = preferredObject | backupObject; (5) + is an addition operator. If the two values involved in the operation are numeric values, the calculation is performed in addition. If both values are strings, the connected strings are connected. If only one value is a string, the other is converted to a string and then connected. For example:
Var num1 = 5, var num2 = 10; var message1 = "The sum of 5 and 10 is" + num1 + num2; alert (message2 ); // "The sum of 5 and 10 is 510" var message2 = "The sum of 5 and 10 is" + (num1 + num2); alert (message2 ); // "The sum of 5 and 10 is 15" (6)-as The binary subtraction operator, if The two values involved in The operation are numerical values, The operation is performed by mathematical subtraction. Otherwise, convert a non-numeric value to a numeric value, and then perform mathematical subtraction.
13. Process Control statements
(1) the for-in statement is used to enumerate all attributes of an object in the following format:
For (property in expression) statement example: // enumerate all properties of the window object for (var propName in window) {document. write (propName);} if the enumerated object is null or undefined, an error is returned. However, the ECMAScript 5 specification does not cause errors, but does not execute the loop body. (2) The label statement is used to identify the statement used later. It is usually referenced by breakcontinue statements in nested loops. The format is as follows:
Label: statement example:
Var num = 0; outermost: for (var I = 0; I <10; I ++) {for (var j = 0; j <10; j ++) {if (I = 5 & j = 5) {break outermost; // terminate the execution of external for statements // (the internal for statement will also be terminated )} num ++ ;}} alert (num); // 55
(3) The with statement can reduce the amount of code, but has poor performance and is not conducive to debugging. Therefore, avoid using it in practice. (4) The expression in the switch statement can be of any data type. The value of case can be not only a constant, but also a variable and expression. The switch statement uses the = Operator to compare values behind the scenes, so it does not force type conversion.
14. Functions
(1) No matter how many form parameters are defined in a function, when calling a function, the number of real parameters passed can be any number, more than, less than, or equal to the number of form parameters, there is no real parameter. All parameters are passed by value, and reference transmission is not allowed. (2) The arguments object can be accessed within the function to obtain the value of each real parameter and the number of parameters. The number of parameters is determined by the real parameter. The arguments object is similar to an array. You can obtain the values of a single parameter through square brackets, such as arguments [0] and arguments [1. If a named parameter exists, the index number ranges from small to large and corresponds to the order of the corresponding parameter. Length is the attribute of arguments, used to obtain the number of real parameters. For example:
Function doAdd () {if (arguments. length = 1) {alert (arguments [0] + 10);} else if (arguments. length = 2) {alert (arguments [0] + arguments [1]) ;}} doAdd (10); // 20 doAdd (30, 20); // 50
(3) The values of the shapes involved in the arguments object are always the same. If a change is made, the values change accordingly, but changes in the strict mode do not affect each other. If the number of arguments is less than the number of arguments, the parameter access value inside the function is undefined. Even if the arguments object is used to assign a value to the parameter, the value will not be changed, it is still undefined, but it can be assigned directly, but these behaviors will be changed in strict mode. For example:
Function testFunc (num1, num2) {// if the call passes only one parameter alert (num1 + "" + arguments [0]); // 5 5 5 arguments [0] = 10; alert (num1 + "" + arguments [0]); // 10 10 num1 = 11; alert (num1 + "" + arguments [0]); // 11 11 alert (num2 + "" + arguments [1]); // undefined arguments [1] = 12; alert (num2 + "" + arguments [1]); // undefined 12 num2 = 13; alert (num2 + "" + arguments [1]); // 13 12} testFunc (5 );
(4) because of the above nature of functions in JavaScript, functions cannot be overloaded, but you can simulate function overloading by checking the type and number of parameters. If multiple cognominal functions are defined successively, the last function with the same name is called. (5) There is no need to specify the type of the return value when the function is defined. You can use the return statement to return the value or return NULL (exit the function) at any time within the function ). A function that does not return a value will actually return undefined.