Appendix D. Reintroducing JavaScript

 

This appendix covers

  • Best practices when writing JavaScript
  • Using JSON effectively to pass data
  • Uncovering callbacks and escaping callback hell
  • Writing modular JavaScript with closures and patterns

JavaScript is such a fundamental part of the MEAN stack that we’re going to spend a little bit of time looking at it. We need to cover the bases because successful MEAN development depends on it. JavaScript is such a common language that it seems everybody knows some of it. This is partly because JavaScript is really easy to get started with, and seems quite forgiving with the way that it’s written. Unfortunately, this looseness and low barrier to entry can encourage bad habits and sometimes have some quite unexpected results.

The aim of this appendix isn’t to teach JavaScript from scratch; if you don’t know JavaScript at all you may struggle and find it hard going. On the other hand, not everybody will need to read this appendix in detail, particularly experienced JavaScript developers. If this is you, it’s probably worth your while skimming through it just in case there’s something new in here.

D.1  Everybody knows JavaScript, right?

D.2  Good habits or bad habits

D.2.1   Variables, functions, and scope

D.2.2   Logic flow and loops

D.2.3   Formatting practices

D.3  Getting to know JSON

D.3.1   A recap on JavaScript object literals

D.3.2   Differences with JSON

D.3.3   Why is JSON so good?

D.4  Understanding JavaScript callbacks

D.4.1   Use setTimeout to run code later

D.4.2   Asynchronous code

D.4.3   Running a callback function

D.4.4   Named callbacks

D.4.5   Callbacks in Node

D.5  Writing modular JavaScript

D.5.1   Closures

D.5.2   Module pattern

D.5.3   Revealing module pattern

D.6  Summary

sitemap