This Repo required for Asac labs class 2
To find the source of an error, it helps to know how scripts are processed.
The order in which statements are executed can be complex; some tasks
cannot complete until another statement or function has been run.
The JavaScript interpreter uses the concept of execution contexts. There is one global execution context; plus, each function creates a new new execution context. They correspond to variable scope.
The JavaScript interpreter processes one line of code at a time. When a statement needs data from another function, it stacks (or piles) the new function on top of the current task.
### EXECUTION CONTEXT & HOISTING Each time a script enters a new execution context, there are two phases of activity:
The new scope is created
Variables, functions, and arguments are created
The value of the this keyword is determined
The preparation phase is often described as taking all of the variables and functions and hoisting them to the top of the execution context.
### lexical scope: Functions in JavaScript are said to have lexical scope. They are linked to the object they were defined within. So, for each execution context, the scope is the current execution context’s variables object, plus the variables object for each parent execution context.
If a JavaScript statement generates an error, then it throws an exception. At that point, the interpreter stops and looks for exception-handl ing code.
Error objects can help you find where your mistakes are and browsers have tools to help you read them.
When an Er ror object is created, it will contain the following properties:
INCORRECT USE OF eval() FUNCTION The eval () function evaluates text through the interpreter and runs it as code.
INCORRECT USE OF URI FUNCTIONS If these characters are not escaped in URls, they will cause an error: / ? & I : ;
VALUE IS UNEXPECTED DATA TYPE This is often caused by trying to use an object or method that does not exist.
Note:
NaN NOT AN ERROR! If you perform a mathematical operation using a value that is not a number, you end up with the value of NaN, not a type error. NOT A NUMBER
DEBUG THE SCRIPT TO FIX ERRORS:
HANDLE ERRORS GRACEFULLY:
Debugging is about deduction: eliminating potential causes of an error.
You can pause the execution of a script on any line using breakpoints. Then you can check the values stored in variables at that point in time.
You can indicate that a breakpoint should be triggered only if a condition that you specify is met. The condition can use existing variables.
You can create a breakpoint
in your code using just the
debugger keyword
TRY
CATCH
FINALLY
you can generate your own errors before the interpreter creates them.
ERROR HANDLING & DEBUGGING If you understand execution contexts (which have two stages) and stacks, you are more likely to find the error in your code.
Debugging is the process of finding errors. It involves a process of deduction.
The console helps narrow down the area in which the error is located, so you can try to find the exact error.
JavaScript has 7 different types of errors. Each creates its own error object, which can tell you its line number and gives a description of the error.
If you know that you may get an error you can handle it gracefully using the try, catch, finally statements. Use them to give your users helpful feedback.