简体   繁体   English

在JavaScript中传递给函数的参数量

[英]Amount of passed parameters to functions in JavaScript

JavaScript is a revelation to me. JavaScript对我来说是一个启示。 I thought it would be like another sort of classical languages like C#, Java, etc. But it didn't. 我认为它会像C#,Java等其他类型的经典语言。但事实并非如此。 "Dynamic world" is tough and unpredictable. “动态世界”艰难且难以预测。 I was astonished when I read that functions can receive as many parameters as you desire. 当我读到函数可以接收到你想要的任意数量的参数时,我感到很惊讶。 Without any error! 没有任何错误! I don't like it at all. 我完全不喜欢它。 I want more "staticness", I want some sort of compile-time errors! 我想要更多“静态”,我想要某种编译时错误!

My question is: am I need to worry about that? 我的问题是:我需要担心吗? Is it a good practice to throw an exception if a quantity of passed parameters are more than a particular function expects? 如果传递的参数数量超过特定函数的预期值,抛出异常是一种好习惯吗?

function foo(one, two, three)
{
    // Is it good?
    if(arguments.length > arguments.callee.length)
        throw new Error("Wrong quantity of arguments in " + arguments.callee.name + "()");

    /* Stuff */
}

foo(1, 2, 3, 4); // -> Error
foo(1, 2, 3);    // -> OK

Should I be concerned about it at all? 我应该关注它吗?

Thanks in advance! 提前致谢!

You probably should not be concerned. 你可能不应该担心。 There is no blanket rule on how to handle errors like this. 如何处理这样的错误没有全面的规则。 It depends entirely upon the type of error and the type of situation. 它完全取决于错误的类型和情况的类型。 In some cases, where it's a serious programming error and there is no way to proceed (like insufficient arguments to perform the desired function), it may make sense to throw an exception or return an error from the function. 在某些情况下,如果它是严重的编程错误并且无法继续(例如执行所需函数的参数不足),则抛出异常或从函数返回错误可能是有意义的。 But, in other cases, an extra argument can just be safely ignored and you can continue on your merry way as if that argument was never passed. 但是,在其他情况下,可以安全地忽略额外的参数,并且可以继续您的快乐方式,就好像该参数从未通过一样。

As you get used to javascript, you will come to understand that many function arguments can be optional and a single function may be correctly called with zero, one, two or three or even N arguments and the code in the function can adapt appropriately. 当你习惯了javascript时,你会发现许多函数参数可以是可选的,并且可以用零,一,二或三或甚至N个参数正确调用单个函数,并且函数中的代码可以适当地调整。 This actually allows you to do things that are not as easy to do in more "static" languages. 这实际上允许您在更多“静态”语言中执行不那么容易的事情。 It is even possible to adapt to the type of the arguments and do something appropriately based on the type of the argument. 甚至可以适应参数的类型,并根据参数的类型做适当的事情。 While this may sound like heresy to someone that only has experience in hard-typed languages, it can actually be extremely useful. 虽然这对于只有硬类型语言经验的人来说听起来像是异端,但它实际上非常有用。

As you maintain a body of code over time, you will also come to find that it's nice to be able to add an argument to the definition of a function, add code to that function that defaults it to a reasonable value if it isn't passed and NOT have to change any of the prior code that was using that function, yet a few new places that need that new argument can start using it immediately. 随着时间的推移你维护一段代码,你也会发现能够在函数定义中添加一个参数,将代码添加到该函数中是很好的,如果它不是,则将其默认为合理的值传递并且不必更改任何使用该函数的先前代码,但是一些需要新参数的新位置可以立即开始使用它。 Rather then grepping through the entire codebase to fix up every caller of that function, you can just make one change in one file and immediately start using a new argument to the function without changing all the other callers. 而不是通过整个代码库来修复该函数的每个调用者,您只需在一个文件中进行一次更改并立即开始使用函数的新参数而不更改所有其他调用者。 This is enormously useful. 这非常有用。


So, in more direct answer to your question, an extra argument passed to a function is never a serious error in javascript. 因此,在更直接地回答您的问题时,传递给函数的额外参数永远不会是javascript中的严重错误。 Your code could just ignore it and proceed. 您的代码可以忽略它并继续。 If you want to alert the developer who wrote that code that an unexpected argument was passed, you can notify them somehow (perhaps some warning text on the debug console) in the "debug" version of your function/library, but I see no reason why you should stop execution in the "production" version of your function/library when you can proceed without any harm. 如果你想提醒编写该代码的开发人员传递了一个意外的参数,你可以在函数/库的“debug”版本中以某种方式通知他们(可能是调试控制台上的一些警告文本),但我认为没有理由为什么你应该在你的功能/库的“生产”版本中停止执行,而不会有任何伤害。

You don't need to worry about this. 你不必担心这个。 If you pass too many arguments, the function will just ignore it. 如果传递的参数太多,函数将忽略它。 You should only throw an error if there are too few arguments. 如果参数太少,您应该只抛出一个错误。 In that case, the function might not be able to run. 在这种情况下,该函数可能无法运行。

While I agree that the number of arguments aren't important (and won't cause a problem so long as you type-check the arguments you're getting before you use them), since an unused, uncalled, argument won't do anything , if you're particularly concerned you could just create a subset of the passed-arguments and access that object internally: 虽然我同意的参数的数量并不重要(只要你不会造成问题类型检查你得到你使用它们之前的参数),因为未使用的,没有道理,争论不会做任何事情 ,如果您特别担心,您可以创建传递参数的子集并在内部访问该对象:

function test(arg1, arg2, arg3) {
    var slice = Array.prototype.slice,
        subset = slice.call(arguments, 0, 3); // depending on how many arguments you want
}

Of course this means that you've now got to recover the parameters from the args object, and since surplus arguments seem to be perfectly safe this seems pointless. 当然这意味着你现在必须从args对象中恢复参数,并且由于多余的参数看起来非常安全,这似乎毫无意义。 But it is still an option. 但它仍然是一种选择。

Albeit unnecessary. 虽然没必要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM