Javascript:配置模式 [英] Javascript: Configuration Pattern

查看:111
本文介绍了Javascript:配置模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题
一个Javascript函数需要很少的参数来处理:

 功能踢(人,理由,金额){
//踢*人* * * *,基于*原因*
}
/ pre>

由于否在JS中执行函数重载的方式,就像你在Java中的操作一样,,如果需要设计以便将来改进(参数添加),可以写成:

  / *函数参数pattern * / 
function kick(){
//以* arguments [0] *的形式踢人,如*参数[1] *,
//基于*参数[2] *中的原因,强度与*参数[3] *
}

  / *对象配置模式* / 
函数kick(config){
//踢的人在* config.perso n *,其数量如* config.amount *,
//基于* config.reason *中的原因,强度如* config.strength *
}

我知道对象配置模式允许增加任何默认属性



所以,问题是:
如果我不需要使用参数来增加任何属性,是否有任何重要的原因使用任何一个提出的解决方案而不是另一个?

解决方案

使用对象有一些优点:



1。代码更可读



考虑以下两个调用:

 踢({user:u,
reason:flood,
log:true,
rejoin:false,
timeout:60000,
privmessage:true}) ;

kick(u,flood,true,false,60000,true);

,并想象有人读取通话。什么是第一个 true ?还要注意,你在几个月内将处于相同的确切位置(不是记住 c> c> c> c的第四个参数是非常类似于知道它)。



2。您可以通过参数



使用对象方法,您可以传递函数一组参数,该函数必须用于调用另一个函数

  function kickgroup(users,parms){
for(var i = 0; i< users.lenght; i ++){
var uparms =的Object.create(PARMS);
uparms.user = users [i];
踢(uparms);
}
}

另请注意,在您不需要使用参数[x] 语法来惩罚自己。您可以声明参数并随着函数进化而添加:任何未被传递的参数将被设置为 undefined (如果您需要,仍然可以访问 arguments.length 以区分调用者是否明确地传递你的函数 undefined )。


Problem: A Javascript function needs few parameters to work with:

function kick(person, reason, amount) {
    // kick the *person* with the *amount*, based on the *reason*
}

As there's no way to do function overloading in JS like how you do in Java, if it needs to be designed for easy future improvement (parameters adding), it can be written as:

/* Function Parameters pattern */
function kick() {
    // kick the person as in *arguments[0]*, with the amount as in *arguments[1]*,
    // based on the reason as in *arguments[2]*, with the strength as in *arguments[3]*
}

or

/* Object Configuration Pattern */
function kick(config) {
    // kick the person as in *config.person*, with the amount as in *config.amount*,
    // based on the reason as in *config.reason*, with the strength as in *config.strength*
}

I do know that Object Configuration Pattern allows augmentation for any default properties.

So, the question is: If I don't need to augment any properties with the parameters, is there any significant reason of using any one of the proposed solutions as opposed to the other?

解决方案

Using an object has a few advantages:

1. The code is more readable

Consider the following two calls:

kick({user: u,
      reason: "flood",
      log: true,
      rejoin: false,
      timeout: 60000,
      privmessage: true});

kick(u, "flood", true, false, 60000, true);

and imagine someone else reading the call. What is the first true? Note also that yourself in a few month would be in the same exact position (not remembering what is the fourth parameter to kick is very similar to not knowing it).

2. You can tunnel parameters

With the object approach you can pass a function a set of parameters that this function must use to call another function

function kickgroup(users, parms) {
    for (var i=0; i<users.lenght; i++) {
        var uparms = Object.create(parms);
        uparms.user = users[i];
        kick(uparms);
    }
}

Note also that in the arguments case you don't need to punish yourself by using arguments[x] syntax. You can just declare the parameters and add them as the function evolves: any parameter that has not been passed will be set to undefined (and if you need you can still access arguments.length to distinguish if the caller explicitly passed your function undefined).

这篇关于Javascript:配置模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆