Wrong practice of accessing an object of arguments for each function that takes parameters?

Let's say I have 300 functions in one script. Is it really bad to refer to the object of arguments in each function, and not by named parameters?

Edit: It’s good that many people ask why I would like to do this, this is really a theoretical question, but since you asked, is there any reason why I would like to do this other than variable parameters?

+4
source share
3 answers

it’s bad practice to write code that is hard to read and hard to understand, maintain or use others.

good practice allows you to specify named arguments and give them meaningful, useful, descriptive names when possible.

In addition, you must remember that you can declare named arguments, even if all of them are not required. You can then check arguments.length to see how many arguments were passed, or check the values ​​of specific named arguments to see if they are undefined or not.

In the case of strongly variable arguments, you can also pass the object and let the object independently describe which arguments were passed.

There are specific cases where an argument object is useful, and using it is the best way to write clear, concise, and safe code, but I have never seen a case where all functions declare named arguments and use only the arguments to the object. Unless you have a very unusual project, this is usually not the best way to code so many functions.

For performance, it seems that access to named arguments is also faster than access to the arguments object in all major browsers. In this performance test, jsperf , which simply sums up the first three arguments passed to the test function using named arguments , is 2-8x faster than using the arguments object. The exact performance difference depends on what the function is trying to execute, but the arguments object definitely looks slower.

If you provide more details on why you want to use the arguments object instead of named arguments, or why you think it would be useful to use the arguments object, then we could offer more specific recommendations.

+12
source

In addition to jfriend00 a great answer ...

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

  • Used in function
  • The function uses eval or a similar construct.

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

[1] V8 in Chrome in particular.

+3
source

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

In lax mode, the argument members are tied to the corresponding formal parameter (if the value is passed), so changing the value of one changes the value of the other.

Not so in strict mode.

eg.

 foo('a'); function foo(arg) { // Change the value of arg arg = 'b'; arg == arguments[0]; // false in strict mode, // true otherwise } 

In addition, arguments.callee and arguments.caller not available in strict mode. Attempting to access them will result in an error.

0
source

Source: https://habr.com/ru/post/1444959/


All Articles