4. Grammar projects
4.1 Data type conversion
Operators are specific to data types. If the operator finds that the operator has a different type than expected, it automatically converts the type.
'4' - '3' / / 1
Copy the code
4.1.1 Forced Conversion
1. Number()
- Primitive type value
The Number function converts a string to a Number, which is much stricter than the parseInt function. Basically, if a single character cannot be converted to a numeric value, the entire string is converted to NaN. ParseInt parses characters one by one, while the Number function converts the string type as a whole
parseInt('42 cats') / / 42
Number('42 cats') // NaN
Copy the code
2. String()
The String method returns a type String if the argument is an object. If it is an array, return the array as a string. The conversion rules behind the String method are essentially the same as for the Number method, except that the order in which valueOf and toString are executed is reversed.
3. Automatic conversion
The rule for automatic conversion is this: when you expect a value of that type, you call the conversion function of that type. For example, if a location is expected to be a String, the String() function is called to convert. If the position can be either a string or a value, it defaults to a value.
4.2 Error Handling Mechanism
- SyntaxError: SyntaxError that occurs when parsing code.
var 1a;
// Uncaught SyntaxError: Invalid or unexpected token
// The parentheses are missing
console.log 'hello');
// Uncaught SyntaxError: Unexpected string
Copy the code
- RangeError: An error that occurs when a value is out of range, if the array length is negative, if the Number object’s method arguments are out of range, or if the function stack exceeds the maximum value.
- TypeError: An error occurs when a variable or parameter is not of the expected type.
new 123
// Uncaught TypeError: 123 is not a constructor
var obj = {};
obj.unknownMethod()
// Uncaught TypeError: obj.unknownMethod is not a function
Copy the code
2 throw statement
The purpose of a throw statement is to manually interrupt program execution and throw an error.
var x = -1;
if (x <= 0) {
throw new Error('x must be positive. ');
}
Copy the code
4.2.2 the try… Catch the structure
As soon as an error occurs, the program is aborted. JavaScript provides try… A catch structure that allows error handling, with the option to proceed.
try {
throw new Error('Wrong! ');
} catch (e) {
console.log(e.name + ":" + e.message);
console.log(e.stack);
}
// Error: Error!
// at
:3:9
/ /...
Copy the code
In the above code, the try block throws an error (this example uses a throw statement), and the JavaScript engine immediately transfers execution to the catch block, or the error is caught by the catch block. Catch takes a parameter that represents the value thrown by the try block.
4.2.3 Finally code block
try… The catch structure allows a finally block to be added at the end to represent the statement that must be run at the end regardless of an error.
function cleansUp() {
try {
throw new Error('Wrong... ');
console.log('This line will not be executed');
} finally {
console.log('Finish the cleanup');
}
}
cleansUp()
// Complete the cleanup
// Uncaught Error: Error......
// at cleansUp (
:3:11)
// at
:10:1
Copy the code