Duplicated Argument Names

Publikováno: 6.1.2020

Oftentimes we override or monkey patch functions and, in many cases, there are arguments we don’t care too much about. A common practice for those arguments is using _ for argument names — it’s a generally accepted and known practice for “this isn’t important”. I started thinking about multiple useless arguments and if you could […]

The post Duplicated Argument Names appeared first on David Walsh Blog.

Celý článek

Oftentimes we override or monkey patch functions and, in many cases, there are arguments we don’t care too much about. A common practice for those arguments is using _ for argument names — it’s a generally accepted and known practice for “this isn’t important”. I started thinking about multiple useless arguments and if you could use the same name for the sake of minification — you can.

So what happens when you use the same argument name more than once? An error? Uses the first value? The last value? Let’s have a look:

function myFunc(_, _, _) {
  console.log("_: ", _);
}

myFunc(1, 2, 3);

// >> 3

The duplicated argument is given the value of the last provided argument.

For some reason I expected an error when using an argument name more than once. On the other end, you can change argument values so I shouldn’t be surprised. Anyway, happy coding!

The post Duplicated Argument Names appeared first on David Walsh Blog.

Nahoru
Tento web používá k poskytování služeb a analýze návštěvnosti soubory cookie. Používáním tohoto webu s tímto souhlasíte. Další informace