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.

References: 

JavaScript Patterns - by Stoyan Stefanov (O`Reilly)

JavaScript Patterns 4.9 Configuration Objects的更多相关文章

  1. JavaScript Patterns 3.8 Error Objects

    The error objects created by constructors(Error(),  SyntaxError(), TypeError(), and others) have the ...

  2. JavaScript Patterns 7.1 Singleton

    7.1 Singleton The idea of the singleton pattern is to have only one instance of a specific class. Th ...

  3. JavaScript Patterns 6.6 Mix-ins

    Loop through arguments and copy every property of every object passed to the function. And the resul ...

  4. JavaScript Patterns 6.5 Inheritance by Copying Properties

    Shallow copy pattern function extend(parent, child) { var i; child = child || {}; for (i in parent) ...

  5. JavaScript Patterns 6.4 Prototypal Inheritance

    No classes involved; Objects inherit from other objects. Use an empty temporary constructor function ...

  6. JavaScript Patterns 6.2 Expected Outcome When Using Classical Inheritance

    // the parent constructor function Parent(name) { this.name = name || 'Adam'; } // adding functional ...

  7. JavaScript Patterns 5.6 Static Members

    Public Static Members // constructor var Gadget = function (price) { this.price = price; }; // a sta ...

  8. JavaScript Patterns 5.3 Private Properties and Methods

    All object members are public in JavaScript. var myobj = { myprop : 1, getProp : function() { return ...

  9. JavaScript Patterns 5.1 Namespace Pattern

    global namespace object // global object var MYAPP = {}; // constructors MYAPP.Parent = function() { ...

随机推荐

  1. 似乎都设置了utf-8,为什么出现乱码

    解决方法如下: ****************************************************************************************[来自我 ...

  2. java中获取文件或文件夹的路径方法

    获取当前类的所在工程路径; 如果不加"/" File f = new File(this.getClass().getResource("").getPath( ...

  3. hibernate----1-1

    <?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hi ...

  4. Oracle数据库,非空约束、主键约束、外键约束、唯一约束

    非空约束:设置列时,可为空默认可为空,去掉对号之后设置数据不可为空: 唯一约束:在键中设置,唯一约束名称.类型Unique.列名:设置应用完成之后,此列数据具有唯一性:即数据不可重复(类型:Uniqu ...

  5. 项目总结笔记系列 Autonomy IDOL Server KT Session1

    在说上面这个图之前,先说一下背景知识. Autonomy地址: http://www.autonomy.com.cn http://www.autonomy.com.cn/content/home/i ...

  6. windows 7/10下安装oracle 10g

    有段时间没搞oracle了,最近要给别人在win 7下装个oracle 10g,特记录备忘下. 使用http://download.oracle.com/otn/nt/oracle10g/10201/ ...

  7. 硬盘空间满导致mysql ibd文件被删后提示Tablespace is missing for table 'db_rsk/XXX"

    昨天一早,开发人员反馈说一个测试环境报Tablespace is missing for table 'db_rsk/XXX",周末刚升级过,特地让开发回去查了下,说脚本中肯定没有drop ...

  8. JavaScript学习(3):函数式编程

    在这篇文章里,我们讨论函数式编程. 什么是函数式编程?根据百度百科的描述,“函数式编程是种编程典范,它将电脑运算视为函数的计算.函数编程语言最重要的基础是 λ 演算(lambda calculus). ...

  9. 百度地图跟angular的结合

    我现在做的一个项目是angular,但是我用直接引用百度地图的方法引进js,写html,js代码,发现,我去,报错了,我一开始还以为是百度地图跟angular有冲突,然后我就去搜索啊,发现angula ...

  10. bootstrap源码分析之scrollspy(滚动侦听)

    源码文件: Scrollspy.js 实现功能 1.当滚动区域内设置的hashkey距离顶点到有效位置时,就关联设置其导航上的指定项2.导航必须是 .nav > li > a 结构,并且a ...