Duplicated Argument Names

By  on  

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. If, however, "use strict" is used, an error will be thrown.

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!

Recent Features

  • By
    JavaScript Promise API

    While synchronous code is easier to follow and debug, async is generally better for performance and flexibility. Why "hold up the show" when you can trigger numerous requests at once and then handle them when each is ready?  Promises are becoming a big part of the JavaScript world...

  • By
    5 HTML5 APIs You Didn’t Know Existed

    When you say or read "HTML5", you half expect exotic dancers and unicorns to walk into the room to the tune of "I'm Sexy and I Know It."  Can you blame us though?  We watched the fundamental APIs stagnate for so long that a basic feature...

Incredible Demos

  • By
    Degradable SELECT onChange

    Whenever I go to Google Analytics I notice a slight flicker in the dropdown list area. I see a button appear for the shortest amount of time and the poof! Gone. What that tells me is that Google is making their site function...

  • By
    MooTools TextOverlap Plugin

    Developers everywhere seem to be looking for different ways to make use of JavaScript libraries. Some creations are extremely practical, others aren't. This one may be more on the "aren't" side but used correctly, my TextOverlap plugin could add another interesting design element...

Discussion

    Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!