Jon Rumsey

An online markdown blog and knowledge repository.


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

Notes from Duckett HTML and JS Books

Notes taken from Ducketts JS book:
Chapter 6: “Error Handling and Debugging” (pp.449-486)

Chapter 6 Error Handling and Debugging

Key Aspects of JS Execution

Context and Scope

Three execution contexts:

  1. Global: Code in the script but not inm a function. Only one of these per code page.
  2. Function: Code running inside a function is bound by the function's braces.
  3. Eval Context: An internal function context.

Two variable scopes:

  1. Global: Variables declared outside a function can be accessed from anywhere.
  2. Function-level: Variables declared inside a function cannot be access from outsite same function.

Execution Context and Hoisting

Two phases of activity when JS enters a new execution context:

  1. Prepare: Scope, variables, functions, and args are created.
  2. Execute: Values assigned to variables, functions can be referenced and executed, and statements are executed.

More About Scoping

Variables Object is used in the interpreter to store aspects of different execution contexts.
Each execution context has access to variables object of its parent.
Create variables as close to where they are needed because the interpreter will 'climb the stack' of contexts to find a more widely scoped variable, which is an expensive operation.
Each function gets its own execution context to run within.

Understanding Errors and Error Objects

The Process:

Error Objects:

There are 7 types of Error Objects in JS:

  1. Error
  2. SyntaxError
  3. ReferenceError
  4. TypeError
  5. RangeError
  6. URIError
  7. EvalError

In summary, either something doesn't exist, was not used correctly, or is outside of a range like number of elements not the correct Type.

Debugging and Dealing with Errors

Console.Table() can be used to show tabular output of objects and multi-dimensional arrays.

console.assert() displays assertion failed messages to the console.

Breakpoints and Stepping Through Code

Breakpoints are spots where code is 'halted' so the state can be inspected. Chrome and Firefox have built-in breakpoint/debugger tools.
Once code hits a breakpoint, the engineer can step through the code lines, in and out of function calls, to learn what is happening during code execution.
Conditional breakpoints can be set so a breakpoint will only be hit under certain conditions.
Breakpoint from your code with debugger; but it requires developer tools are open in the browser.

Handling Exceptions

Try block: wrap this around code that you think will (or does) throw an exception. Catch block: Required? Wrap this around code that will do something (intelligent and helpful) with the application state or handle the situation gracefully and allow code execution to continue.
Finall block: Optional? Used to 'clean up' after a try block has been entered and exited, regardless of whether or not an Exception was thrown.

Throwing Errors

Keyword throw will raise an Exception along with a String message.
Usage: throw new Error('message');

Debugging Tips

Things to try when debugging errors, exceptions, and unexpected results:

Resources

jQuery debugger (Check the Chrome Store)
JSBin.com
JSFiddle.com
Dabblet.com
CSSDeck.com
CodePen.com
JSLint.com
JSHint.com
JSONLint.com

Back to index in readme