background preloader

Essential Node.js patterns and snippets

Essential Node.js patterns and snippets
In this post, I take a look at the different patterns that you need to know when using Node.js. These came from my own coding and from a look at the code behind Tim Caswell’s flow control libraries. I think it is necessary to know how these basic patterns are implemented even if you use a library.. 1.1 Class pattern If the class is long, then instead of doing a single Class.prototype = {…} assignment, it may be split into multiple Class.prototype.method = function () {..} assignments. Reminder: Assign all your properties some value in your constructor. 1.2 Accessing global values from objects // constructorvar Class = function(global, value2) { = global;}// access using in class methods 1.3 Factory pattern // Constructorvar Class = function(value1, value2) { ... }// FactoryClass.factory(value1) { return new Class(value1, "aaa"); }// properties and methodsClass.prototype = { ... }; 1.4 Sharing state between modules 1.5 Singleton class (added Feb 2011) 2.1 Parsing GET 4.

Top News - Echo JS Javascript Version française You are about to download a document protected by copyright law in your country. The unauthorized reproduction or distribution of this copyrighted work is illegal and may be punishable by criminal law. The document is a single-user, non-revisable Adobe Acrobat® PDF file. You may print out and retain one-only printed copy of the PDF file. This printed copy is fully protected by national and international copyright laws, and may not be photocopied or reproduced in any form. While all reasonable care is taken in the preparation and review of ISO International Standards and other deliverables, ISO does not warrant that the content of the document is accurate or up to date or that the document will be suitable for your purposes. To the extent allowed in applicable law, in no event shall ISO be liable for any direct, indirect, punitive, incidental, special, consequential damages, or any damages whatsoever arising out of or connected with the use or misuse of this document.