Final thoughts

JavaScript is a forgiving language, which makes it easy to learn, but it’s also easy to pick up bad habits. If you make a little mistake in your code, JavaScript sometimes thinks, “Well, I think you meant to do this, so that’s what I’ll go with.” Sometimes it’s right, and sometimes it’s wrong. This isn’t acceptable for good code, so it’s important to be specific about what your code should do, and you should try to write your code in the way that the JavaScript interpreter sees it.

A key to understanding the power of JavaScript is understanding scope: global scope and function scope and lexical scope. There are no other types of scope in JavaScript. You want to avoid using the global scope as much as possible, and when you do use it, try to do it in a clean and contained way. Scope inheritance cascades down from the global scope, so it can be difficult to maintain if you’re not careful.

JSON is born of JavaScript but isn’t JavaScript; it’s a language-independent data exchange format. JSON contains no JavaScript code and can quite happily be passed between a PHP server and a .NET server; JavaScript isn’t required to interpret JSON.

Callbacks are vital to running successful Node applications, because they allow the central process to effectively delegate tasks that could hold it up. To put it another way, callbacks enable you to use sequential synchronous operations in an asynchronous environment. But callbacks aren’t without their problems. It’s easy to end up in callback hell, having multiple nested callbacks with overlapping inherited scopes making your code hard to read, test, debug, and maintain. Fortunately, you can use named callbacks to address this problem on all levels so long as you remember that named callbacks don’t inherit scope like their inline anonymous counterparts.

Closures and module patterns provide ways to write code that’s self-contained and reusable between projects. A closure enables you to define a set of functions and variables within its own distinct scope, which you can come back to and interact with through the exposed methods. This leads to the revealing module pattern, which is convention-driven to draw specific lines between what’s private and what’s public. Modules are perfect for writing self-contained pieces of code that can interact well with other code, not tripping up over any scope clashes.

Recent changes to the JavaScript specification, such as the addition of class syntax and greater emphasis on functional programming, flesh out the available toolkit to suit whichever style of code you want to use.

A great many other additions to the JavaScript specification aren’t covered here: the rest operator, the spread operator, and generators, to name a few. It’s an exciting time to be working with the JavaScript language.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.224.0.25