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

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

问题描述

问题
Javascript函数需要很少的参数:

 函数踢(人,理性,金额){
//根据*原因*踢*人* *
}

由于没有方法是在JS中执行函数重载,就像你在Java中做的那样,如果需要为将来的改进(参数添加)而设计,它可以写成:

  / *函数参数pattern * / 
function kick(){
//以* arguments [0]如* arguments [1] *,
//基于原因在* arguments [2] *,强度如* arguments [3] *
}

  / *对象配置模式* / 
function kick(config){
//以* config.person *的形式引发用户,数量为* config.amount *,
//在* config.reason *中的原因,强度如* config.strength *
}

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



因此,问题是:
如果我不需要使用参数扩充任何属性

解决方案

使用任何一个建议解决方案的任何重要原因使用对象有几个优点:



1。代码更易读



请考虑以下两个调用:

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

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

并设想其他人阅读通话。第一个 true 是什么?还要注意,你自己在几个月内会处于相同的确切位置(不记住 kick 的第四个参数是非常相似的知道它)。



2。您可以对参数进行隧道传输



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

  function kickgroup(users,parms){
for(var i = 0; i< users.lenght; i ++){
parms.user = users [i];
kick(parms);然而,一个问题是,$ c
$ b}


$ b $ c> parms
对象将被修改(通过使用 parms 中的对象derived来解决这个问题。 p>

注意,在 arguments 情况下,你不需要使用 arguments [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++) {
        parms.user = users[i];
        kick(parms);
    }
}

one problem with this however is that the parms object is going to be modified (there is a solution for this issue by using an object "derived" from parms.

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天全站免登陆