reading-notes

This Repo required for Asac labs class 2


Project maintained by ManarAbdelkarim Hosted on GitHub Pages — Theme by mattgraham

Debugging

ORDER OF EXECUTION :

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. ORDER OF EXECUTION

EXECUTION CONTEXTS

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 STACK

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:

  1. PREPARE
  1. EXECUTE
    • Now it can assign values to variables
    • Reference functions and run their code
    • Execute statements

hoisting

The preparation phase is often described as taking all of the variables and functions and hoisting them to the top of the execution context.

UNDERSTANDING SCOPE

### 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.

UNDERSTANDING ERRORS

exception:

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.

exception

ERROR OBJECTS:

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:

ERROR OBJECTS

Error object types:

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

HOW TO DEAL WITH ERRORS

  1. DEBUG THE SCRIPT TO FIX ERRORS:

    • you will need to debug the code, track down the source of the error, and fix it.
  2. HANDLE ERRORS GRACEFULLY:

    • You can handle errors gracefully using try, catch, throw, and f i na 1 ly statements.

A DEBUGGING WORKFLOW :

Debugging is about deduction: eliminating potential causes of an error.

BREAKPOINTS:

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.

CONDITIONAL BREAKPOINTS

You can indicate that a breakpoint should be triggered only if a condition that you specify is met. The condition can use existing variables.

DEBUGGER KEYWORD:

You can create a breakpoint in your code using just the debugger keyword

HANDLING EXCEPTIONS

HANDLING EXCEPTIONS

THROWING ERRORS

you can generate your own errors before the interpreter creates them.

SUMMARY