preface
There are seven error types defined in JavaScript:
Error
EvalError
RangeError
ReferenceError
SyntaxError
TypeError
URIError
For details, please refer to MDN, ReferenceError and TypeError as common types encountered in development. Here I make a distinction
LHS and RHS
When we execute the code a = 2, the engine will perform an LHS query for variable A. The corresponding RHS query is simply understood as the left and right side of the assignment operation. To be more precise, RHS is to find the value of a variable, LHS is the container to find the variable, and RHS can be said to be “non-left”.
console.log(a);
b();
Copy the code
The A and B here are RHS queries
ReferenceError
ReferenceError is raised when an RHS query on a variable does not find the desired variable in the scope, as in the example above, if a is not defined, an error is raised
console.log(a) // Uncaught ReferenceError: a is not defined
Copy the code
Unlike RHS, if a variable is not defined in LHS, it is created in global scope, provided it is run in non-strict mode
a = 2;
consol.log(window.a);
Copy the code
TypeError
The engine will raise TypeError if the RHS queries for a variable and you try to do something improper with the variable, such as calling a non-function type and querying for an attribute of null and undefined
var a = 1
a() // Uncaught TypeError: a is not a function
console.log(a.info.name); // Uncaught TypeError: Cannot read property 'name' of undefined
Copy the code
Hidden operations
When the function is called, we pass in an argument that hides an LHS and an RHS
function foo(a) {}
foo(b)
Copy the code
Foo actually executes var a = b once in foo, which contains an RHS and LHS, and raises a ReferenceError if b is not defined
reference
JavaScript You Don’t Know (Volume 1)