首页 > 网站 > WEB开发 > 正文

JavaScript Patterns 4.9 Configuration Objects

2024-04-27 14:22:35
字体:
来源:转载
供稿:网友

javaScript Patterns 4.9 Configuration Objects

2014-06-17 09:00 by 小郝(Kaibo Hao), ... 阅读, ... 评论, 收藏, 编辑

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)


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表