Principle • Aligning each case with switch(an exception to the curly braces indentation rule). • Indenting the code within each case. • Ending each case with a clear break;. • Avoiding fall-throughs (when you omit the break intentionally). If you're…
Chaining Pattern - Call methods on an object one after the other without assigning the return values of the previous operations to variables and without having to split your calls on multiple lines. var obj = { value: 1, increment: function () { this…
Drawbacks of the namespacing pattern • Reliance on a single global variable to be the application’s global. In the namespacing pattern, there is no way to have two versions of the same application or library run on the same page, because they both ne…
function writeCode(callback) { // do something... callback(); // ... } function introduceBugs() { // ... make bugs } writeCode(introduceBugs); A Callback Example // refactored findNodes() to accept a callback var findNodes = function (callback) { var…
Gets a length property containing the number of arguments the function expects: function func(a, b, c) {} console.log(func.length); var myFunc = function () { // serialize the arguments object as a JSON string and use that string as a key in your cac…
7.1 Singleton The idea of the singleton pattern is to have only one instance of a specific class. This means that the second time you use the same class to create a new object, you should get the same object that was created the first time. var obj =…
Scenario You want to use just the methods you like, without inheriting all the other methods that you’ll never need. This is possible with the borrowing methods pattern, which benefits from the function methods call() and apply(). // call() example…
Shallow copy pattern function extend(parent, child) { var i; child = child || {}; for (i in parent) { if (parent.hasOwnProperty(i)) { child[i] = parent[i]; } } return child; } Deep copy pattern function extendDeep(parent, child) { var i, toStr = Obje…