Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose/benefit of using ignored parameters in a JavaScript function?

Just so there is no misunderstanding, this question is not about allowing for optional parameters in a JS function.

My question is motiviated by the jQuery parseXML function, which is defined in jQuery.js as follows:

// Cross-browser xml parsing
// (xml & tmp used internally)
parseXML: function( data, xml, tmp ) { 
   ... 
} 

Within the body of the function, the parameters xml and and tmp are both assigned before they are used. That means they are being used as local variables, so the function could have been defined like this:

parseXML: function(data) { 
   var xml, tmp;
   ... 
}

What is the benefit of doing it the first way, other than saving a few characters in the minified version of jQuery.js?

like image 403
Joel Lee Avatar asked Apr 01 '11 22:04

Joel Lee


1 Answers

If we define two functions...

function a ( foo ) { }
function b ( foo, bar, baz ) {}

...they'll report different lengths...

console.log( [a.length, b.length] ); // logs [1, 3]

It's very rare to see this little known feature of javascript used.

But apart from shaving a couple of bytes off the minified file-size, this is the only other reason that I can think of.

like image 188
Már Örlygsson Avatar answered Sep 21 '22 12:09

Már Örlygsson