Javascript 函数参数处理

发布于 2024-09-25 02:19:16 字数 963 浏览 3 评论 0原文

处理 javascript 函数中的参数的最佳方法似乎是要求调用者传递一个关联数组:

example = function(options) {
  alert('option1: ' + options.a + ', option2: ' + options.b);
}

现在参数已命名,并且在调用此函数时不需要按任何特定顺序:

example({'b':'hello option2', 'a':'hello option1'});

我唯一不喜欢的是我有让所有这些额外的代码来处理必需的和默认的参数,更不用说不允许额外的参数,这样调用者就知道他们调用了错误的函数:

example = function(options) {
var required_args = ['a', 'd'];
var default_args = {'b':'option2', 'c':'option3'};
var allowed_args = ['a', 'b', 'c', 'd'];
// check required args
// check allowed args
for (var arg in default_args) {
    if (typeof options[arg] == "undefined")
              options[arg] = default_args[arg];
  }
  alert('option1: ' + options.a + ', option2: ' + options.b);
}

是否有标准的方法来处理这个问题?我想我可以创建一个类似的函数:

deal_with_args(options, required_args, default_args, allowed_args)

如果违反了 required_args 或 allowed_args 则抛出一些异常...

It seems the best way to deal with arguments in javascript functions is to require the caller to pass an associative array:

example = function(options) {
  alert('option1: ' + options.a + ', option2: ' + options.b);
}

Now arguments are named and not needed in any particular order when calling this function:

example({'b':'hello option2', 'a':'hello option1'});

The only thing I don't like is I have to have all this extra code to deal with required and default arguments, not to mention not allowing extra arguments so the caller knows they called the function wrong:

example = function(options) {
var required_args = ['a', 'd'];
var default_args = {'b':'option2', 'c':'option3'};
var allowed_args = ['a', 'b', 'c', 'd'];
// check required args
// check allowed args
for (var arg in default_args) {
    if (typeof options[arg] == "undefined")
              options[arg] = default_args[arg];
  }
  alert('option1: ' + options.a + ', option2: ' + options.b);
}

Is there a standard way to deal with this? I guess I can create a function like:

deal_with_args(options, required_args, default_args, allowed_args)

And throw some exception if required_args or allowed_args is violated...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一曲琵琶半遮面シ 2024-10-02 02:19:16
  1. 为什么你想要以任意顺序进行参数?这会让阅读代码的人感到困惑
  2. 函数不应该有很多参数 - 这暗示它做了太多
  3. “必需”和“默认”参数的常见模式对我来说似乎并不那么糟糕。

“标准”形式类似于:

function myFunc(required_param, some_param, param_with_default) {
  // obviously this needs modifiction (ie use 'typeof' as you did) 
  // if you're dealing potentially 'falsey' values
  param_with_default = param_with_default || 'some default value';
  // same caution applies here
  if(!required_param) {
    // throw some exception here, or return an error code, or whatever
  }

  // function code
}
  1. Why do you want to have arguments in an arbitrary order? This is just confusing for people reading your code
  2. A function shouldn't have a lot of arguments - it's a hint that it is doing too much
  3. The usual pattern for 'required' and 'default' params doesn't seem so bad to me.

The 'standard' form is something like:

function myFunc(required_param, some_param, param_with_default) {
  // obviously this needs modifiction (ie use 'typeof' as you did) 
  // if you're dealing potentially 'falsey' values
  param_with_default = param_with_default || 'some default value';
  // same caution applies here
  if(!required_param) {
    // throw some exception here, or return an error code, or whatever
  }

  // function code
}
孤独难免 2024-10-02 02:19:16

您可以对所需的参数进行空合并吗?

alert('option1: ' + (options.a || default_args[a]) + ', option2: ' + (options.b || default_args[b]));

(编辑以更正操作员错误)

Could you do null coalescing for the required args?

alert('option1: ' + (options.a || default_args[a]) + ', option2: ' + (options.b || default_args[b]));

(Edited to correct the operator error)

好多鱼好多余 2024-10-02 02:19:16

我认为将参数的哈希值传递给 javascript 中的函数是有效的,特别是对于 UI 组件的构造函数等。在这些情况下,有时您可能会得到数十个需要调整的参数,所以我认为这不是 将 args 作为散列传递总是一个坏主意。

在这些情况下,我认为明智的做法是拥有一个现成的合理默认值的哈希值,然后用传递的对象扩展它。

我想大多数人会在他们选择的框架中使用扩展方法(原型中的 Object.extend(obj1, obj2)、jQuery.extend(obj1, obj2,...objn) 等)来做到这一点。在幕后,这些方法只是执行 for in 并复制属性。

I think it can be valid to pass hashes of params to functions in javascript, particularly for constructors for UI components etc. In these cases you can sometimes end up with dozens of parameters for which need tweaking so I don't think it's always a bad idea to pass args as a hash.

In these cases I think it's smart to have a ready made hash of sensible defaults then extend it with your passed object.

I guess most people would do this with the extend method in their framework of choice (Object.extend(obj1, obj2) in prototype, jQuery.extend(obj1, obj2,...objn) etc. Under the hood these methods are just doing a for in and copying properties.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文