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
?
If we define two functions...
function a ( foo ) { }
function b ( foo, bar, baz ) {}
...they'll report different length
s...
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With