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

Incredible Demos

  • By
    MooTools onLoad SmoothScrolling

    SmoothScroll is a fantastic MooTools plugin but smooth scrolling only occurs when the anchor is on the same page. Making SmoothScroll work across pages is as easy as a few extra line of MooTools and a querystring variable. The MooTools / PHP Of course, this is a...

  • By
    Facebook Open Graph META Tags

    It's no secret that Facebook has become a major traffic driver for all types of websites.  Nowadays even large corporations steer consumers toward their Facebook pages instead of the corporate websites directly.  And of course there are Facebook "Like" and "Recommend" widgets on every website.  One...

Discussion

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