最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Is it a bad practice to access the arguments object to every function that takes parameters? - Stack Overflow

programmeradmin6浏览0评论

Let's say I have 300 functions in one script. Is it bad to access the arguments object within each function as opposed to passing in named parameters?

Edit: Okay a lot of you are asking why I would want to do this, it's really a theoretical question but since you asked is there any reason why i would want to do this, other than variable parameters?

Let's say I have 300 functions in one script. Is it bad to access the arguments object within each function as opposed to passing in named parameters?

Edit: Okay a lot of you are asking why I would want to do this, it's really a theoretical question but since you asked is there any reason why i would want to do this, other than variable parameters?

Share Improve this question edited Nov 9, 2012 at 1:59 Mr. Smee asked Nov 8, 2012 at 23:11 Mr. SmeeMr. Smee 96012 silver badges28 bronze badges 3
  • 3 If you do this, it's a lot harder to tell at a glance what parameters a function takes, and what they are for. – Cameron Commented Nov 8, 2012 at 23:13
  • 4 The arguments object is useful if an uncertain number of parameters can be passed, otherwise it makes no sense using it. And most of the time you can use an object instead. – elclanrs Commented Nov 8, 2012 at 23:15
  • "Let's say I have 300 functions in one script." - For 300 it is bad practice. For anything less than 300 it is good practice. (OK, seriously, I don't think the number of functions is relevant. It is good practice to use good practices regardless of the scale of the application. Why would you even want to define your functions like that? It only makes sense for functions that do something with a variable number of arguments, e.g., an average() function that returns the average of all arguments.) – nnnnnn Commented Nov 8, 2012 at 23:48
Add a ment  | 

3 Answers 3

Reset to default 16

It is bad practice to write code that is hard to read and hard for others to understand, maintain or use.

It is good practice to specify named arguments and give them meaningful, useful, descriptive names whenever you can.

Also, you should remember that you can declare named arguments even if all are not required. You can then either test arguments.length to see how many arguments were passed or test the values of the specific named arguments to see if they are undefined or not.

In cases of highly variable arguments, you can also pass an object and let the object self-describe what arguments were passed.

There are specific cases when the arguments object is useful and when using it is the best way to write clear, concise and safe code, but I've never seen a case where all functions declare no named arguments and use only the arguments object. Unless you have some very unusual project, this would generally not be the best way to code for so many functions.

For performance, it appears that accessing named arguments is also faster than accessing the arguments object in all the major browsers. In this jsperf performance test that just sums the first three arguments passed to a test function, using the named arguments was 2-8x faster than using the arguments object. The exact performance difference likely depends upon exactly what the function is attempting to do, but the arguments object definitely looks slower.

If you provide more information on the reason why you want to use the arguments object instead of named arguments or why you think it would be beneficial to use the arguments object, then we might be able to offer more concrete advice.

In addition to jfriend00's excellent answer ...

Some of the modern Javascript engines[1] do not create the arguments variable unless it is

  • Used in the function
  • The function uses eval or similar construct

Thus, using arguments can slow down a function simply by using it.

[1] V8 in Chrome, in particular.

It is worth noting that strict mode affects the arguments object.

In non–strict mode, argument members are bound to their corresponding formal parameter (if a value has been passed), so changing the value of one changes the value of the other.

Not so in strict mode.

e.g.

foo('a');

function foo(arg) {

  // Change the value of arg
  arg = 'b';           

  arg == arguments[0];  // false in strict mode, 
                        // true otherwise
}

Also, arguments.callee and arguments.caller are not available in strict mode. Attempting to access them will throw an error.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论