• Complain

John Paxton - JavaScript Cookbook, 3rd Edition

Here you can read online John Paxton - JavaScript Cookbook, 3rd Edition full text of the book (entire story) in english for free. Download pdf and epub, get meaning, cover and reviews about this ebook. year: 2020, publisher: OReilly Media, Inc., genre: Home and family. Description of the work, (preface) as well as reviews are available. Best literature library LitArk.com created for fans of good reading and offers a wide selection of genres:

Romance novel Science fiction Adventure Detective Science History Home and family Prose Art Politics Computer Non-fiction Religion Business Children Humor

Choose a favorite category and find really read worthwhile books. Enjoy immersion in the world of imagination, feel the emotions of the characters or learn something new for yourself, make an fascinating discovery.

John Paxton JavaScript Cookbook, 3rd Edition

JavaScript Cookbook, 3rd Edition: summary, description and annotation

We offer to read an annotation, description, summary or preface (depends on what the author of the book "JavaScript Cookbook, 3rd Edition" wrote himself). If you haven't found the necessary information about the book — write in the comments, we will try to find it.

John Paxton: author's other books


Who wrote JavaScript Cookbook, 3rd Edition? Find out the surname, the name of the author of the book and a list of all author's works by series.

JavaScript Cookbook, 3rd Edition — read online for free the complete book (whole text) full work

Below is the text of the book, divided by pages. System saving the place of the last page read, allows you to conveniently read the book "JavaScript Cookbook, 3rd Edition" online for free, without having to search again every time where you left off. Put a bookmark, and you can go to the page where you finished reading at any time.

Light

Font size:

Reset

Interval:

Bookmark:

Make
JavaScript Cookbook 3E By John Paxton Adam D Scott and Shelley Powers - photo 1
JavaScript Cookbook, 3E

By John Paxton, Adam D. Scott, and Shelley Powers

Copyright 2021 John Paxton, Adam Scot, Shelley Powers. All rights reserved.

Printed in the United States of America.

Published by OReilly Media, Inc. , 1005 Gravenstein Highway North, Sebastopol, CA 95472.

OReilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://oreilly.com). For more information, contact our corporate/institutional sales department: 800-998-9938 or corporate@oreilly.com .

  • Editors: Simon St. Laurent and Brian MacDonald
  • Production Editor: Kara Ebrahim
  • Copyeditor: Jasmine Kwityn
  • Proofreader: Kara Ebrahim
  • Indexer: Judy McConville
  • Interior Designer: David Futato
  • Cover Designer: Ellie Volckhausen
  • Illustrator: Rebecca Demarest
  • July 2010: First Edition
  • February 2015: Second Edition
  • November 2020: Third Edition
Revision History for the Early Release
  • 2020-06-03: First release

See http://oreilly.com/catalog/errata.csp?isbn=9781491901885 for release details.

The OReilly logo is a registered trademark of OReilly Media, Inc. JavaScript Cookbook, 2E, the cover image, and related trade dress are trademarks of OReilly Media, Inc.

While the publisher and the authors have used good faith efforts to ensure that the information and instructions contained in this work are accurate, the publisher and the authors disclaim all responsibility for errors or omissions, including without limitation responsibility for damages resulting from the use of or reliance on this work. Use of the information and instructions contained in this work is at your own risk. If any code samples or other technology this work contains or describes is subject to open source licenses or the intellectual property rights of others, it is your responsibility to ensure that your use thereof complies with such licenses and/or rights.

978-1-492-05568-6

Chapter 1. Errors
A note for Early Release readers

With Early Release ebooks, you get books in their earliest formthe authors raw and unedited content as they writeso you can take advantage of these technologies long before the official release of these titles.

This will be the fourth chapter of the final book. Please note that the GitHub repo will be made active later on.

Errors. Everyone has them. No one is perfect. Things go wrong. Sometimes the error could have been anticipated, other times, not so much. To modify a cliche, its not so much the error as what you do with it that matters.

What should we do with our errors, then? The default behavior is for JavaScript to die at the point of the error, exiting with a stack trace. We can capture an error, react to it, modify it, re-throw it, even hide it if we choose. Returning to a theme of this cookbook: just because we can, does not mean we should. Our guiding light is effective, efficient, reusable JavaScript programming. Lets look at Errors under this light.

JavaScript is not robust at error handing in general. The language has the Error object, which is flexible enough. But catching errors with try-catch lacks features found in other languages. We cannot catch errors by their type. We cannot (easily) filter by error. Further, in the browser, it is rare that we can recover from an error. Rare enough that the recipes below deal with the few common error situations from which an application can recover. In Node.js, throwing errors causes enough problems that an entirely different pattern of error handling involed. We will look at Nodes peculiar form of error handling as well.

Our goal should be to make our programming experience better. Instead of our JavaScript engine dying horribly with an incomprehensible error, we can add and modify information about the error, illuminating the path for the person (likely ourselves) tasked with fixing the error. Allow the failure to happen, but clarify why the error occurred and who raised it.

1.1 Using Errors

JavaScript has eight Error types. The parent type is the aptly-named Error. There are seven subtypes, which we will look at in the next recipe. What does an Error give us? We can count on three properties: a constructor, name, and message. These are all available to subclasses of Error as well. We also have access to a toString() method which will usually return the message property. If we are using the V8 JavaScript engine (Chrome, Node.js, possibly others), we can use captureStackTrace() as well. More on this soon. Lets start with a standard JavaScript Error object.

Problem

You want to create, throw, and catch a standard error

Solution

Create an instance of Error, or a subtype, throw it, and catch it later on. Perhaps that is too brief a description. Start by creating an instance of Error. The constructor takes a string as an argument. Pass something useful and indicative of what the problem was. Consider keeping a list of the various error strings used in the application so that they are consistent and informative. Use the throw keyword to throw the instance you created. Then catch it in a try-catch block.

functionwillThrowError(){if(/* something goes wrong */){// Create an error with useful information.// Don't be afraid to duplicate something from the stack trace, like the method namethrownewError(`Problem in${method},${reason}`);}}// Somewhere else in your codetry{willThrowError();// Other code will not be reached}catch(error){console.error('There was an error: ',error);}
Discussion

We can create an error either with the new keyword before the Error or not. If Error() is called without new preceding it, Error() acts as a function which returns an Error object. The end result is the same. The Error constructor takes one standard argument: the error message. This will be assigned to the errors message property. Some engines allow for additional non-standard arguments to the constructor, but these are not part of a current ECMAScript spec and are not on track to be on a future one. Wrapping the code that will throw an error in a try-catch block allows us to catch the error within our code. Had we not done so, the error would have propagated to the top of the stack and exited JavaScript. Here, we are reporting the error to the console with console.error. This is more or less the default behavior, though we can add information as part of the call to console.error.

1.2 Capturing Errors by their subtypes

JavaScript has seven subtypes of Error. We can check for a subtype of error to determine the kind of error raised by a problem in our code. This may illuminate the possibility of recovery, or at least give us a little more information about what went wrong.

Next page
Light

Font size:

Reset

Interval:

Bookmark:

Make

Similar books «JavaScript Cookbook, 3rd Edition»

Look at similar books to JavaScript Cookbook, 3rd Edition. We have selected literature similar in name and meaning in the hope of providing readers with more options to find new, interesting, not yet read works.


Reviews about «JavaScript Cookbook, 3rd Edition»

Discussion, reviews of the book JavaScript Cookbook, 3rd Edition and just readers' own opinions. Leave your comments, write what you think about the work, its meaning or the main characters. Specify what exactly you liked and what you didn't like, and why you think so.