JavaScript Patterns 4.9 Configuration Objects

Configuration Objects

Passing a large number of parameters is not convenient. A better approach is to substitute all the parameters with only one and make it an object.

var conf = {

    username: "batman",

    first: "Bruce",

    last: "Wayne"

};

addPerson(conf);

Pros


 Cons


• No need to remember the parameters and their order

• You can safely skip optional parameters

• Easier to read and maintain

• Easier to add and remove parameters


• You need to remember the names of the parameters

• Property names cannot be minified

This pattern could be useful when your function creates DOM elements.

JavaScript Patterns 4.9 Configuration Objects

时间: 2024-07-29 01:06:23

JavaScript Patterns 4.9 Configuration Objects的相关文章

JavaScript Patterns 3.7 Primitive Wrappers

Primitive value types: number, string, boolean, null, and undefined. // a primitive number var n = 100; console.log(typeof n); // "number" // a Number object var nobj = new Number(100); console.log(typeof nobj); // "object"  One reason

JavaScript Patterns 7.1 Singleton

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 =

JavaScript Patterns 2.12 Writing API Docs

Free and open source tools for doc generation: the JSDoc Toolkit (http://code.google.com/p/jsdoc-toolkit/) and YUIDoc (http://yuilibrary.com/projects/yuidoc). Process of generating API documentation ? Writing specially formatted code blocks ? Running

JavaScript Patterns 3.6 Regular Expression Literal

1. Using the new RegExp() constructor // constructor var re = new RegExp("\\\\", "gm"); 2. Using the regular expression literal // regular expression literal var re = /\\/gm;  when using the RegExp()constructor, you also need to escape

JavaScript Patterns 2.10 Naming Conventions

1. Capitalizing Constructors var adam = new Person(); 2. Separating Words camel case - type the words in lowercase, only capitalizing the first letter in each word. upper camel case, as in  MyConstructor(), lower  camel  case,  as  in  myFunction(),

JavaScript Patterns 5.3 Private Properties and Methods

All object members are public in JavaScript. var myobj = { myprop : 1, getProp : function() { return this.myprop; } }; console.log(myobj.myprop); // `myprop` is publicly accessible console.log(myobj.getProp()); // getProp() is public too The same is

JavaScript Patterns 2.3 - For loops

HTMLCollections are objects returned by DOM methods such as: ? document.getElementsByName() ? document.getElementsByClassName() ? document.getElementsByTagName() HTMLCollections, which were introduced before the DOM standard and are still in use toda

JavaScript Patterns 6.5 Inheritance by Copying Properties

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

JavaScript Patterns 5.6 Static Members

Public Static Members // constructor var Gadget = function (price) { this.price = price; }; // a static method Gadget.isShiny = function () { // this always works var msg = "you bet"; // Checking if the static method is called by instance. if (t