Seleziona una pagina
sabato, Mar 14

How to Design and Create Node.js Modules (JavaScript Development Series Book 2) (English Edition)


Price:
(as of – Details)



The purpose of this book is to show you how to design and create node.js modules. Node.js modules are external functions or objects that you can integrate into your project. You can think of them as a library. They are written in separate JavaScript files and can be assigned to a variable in your main file with one line of code. If you know how to write JavaScript you know to create a module.

Why Modules?

Node.js projects can grow very quickly to the point where they will no longer fit into one *.js file. Modules are a way to break the code up into manageable and testable chunks. With browser based JavaScript, HTML script tags are one way to pull multiple source files into one place. Node.js doesn’t have that option. Instead, it relies on the built-in require module to pull code from one JavaScript file into another.

What this Book Covers

This book walks through the steps of designing and creating node.js modules. It starts with how to convert JavaScript objects into modules and export them for external use. Other areas that this book covers include: naming exports, constructor parameters, properties, methods, inheritance, callbacks, promises and unit testing.

Here is a more detailed breakdown of the book by chapter:

  • Objects as Modules – The book starts with the creation of a simple module which returns a single object. A factory based module is introduced that can create multiple objects. Learn how to seal an object to protect it from changes.
  • Properties – The next step after defining objects is to add properties. Learn how to add properties that can be enumerated and protected against invalid values.
  • Methods – Methods are what functions are called when they are part of an object. Define methods that can accept named parameters. Learn how to chain methods together and guard against invalid parameters.
  • Module Parameters – Modules can be passed parameters, just like a functions or methods. Learn how to pass parameters to a module or a factory function to construct objects.
  • Named Exports – Instead of returning the whole module, only return part of it. Learn how to export named objects and functions and special techniques for naming factory related exports.
  • Inheritance – Modules and objects can be derived from other modules and objects. Learn how to derive and combine objects and override methods in parent objects.
  • Callbacks – When using third party libraries with callbacks, care must be taken to integrate them into a module. Learn how to create modules that wrap existing callbacks and define new callbacks.
  • Promises – Callbacks calling callbacks calling callbacks can get out of hand. Learn how to create modules that wrap third party callbacks in promises and define new promises.
  • Unit Testing – When making changes to a module it is handy to be able to run a suite of tests to make sure that nothing broke. It’s also helpful to make sure that new functionality works as expected. Learn how to create and run unit tests against modules.