I was reverse engineering a jQuery plugin and found a peculiar function definition.
function myFunction(value, undefined){
...
}
Of course, this works but similar declarations like
function myFunction(value, null) {
...
}
OR
function myFunction(value, 5) {
...
}
do not work.
The initial declaration where the parameter is undefined works like a default parameter value (refer the snippet below). Can someone point to this phenomenon in ECMA6 specification. I have clue on what to even search for.
function strange (value, undefined) {
document.getElementById("output").innerHTML += "<br/>" + "- " + arguments[1];
}
strange(5,6);
strange(5);
<div id="output"></div>
Oddly, undefined is not a reserved keyword and its global value can be overridden (or could be in the past, in some runtime environments). Thus that trick provides a way to wrap code expecting undefined to really be undefined: when that function is called, if only one parameter is passed the second argument, called "undefined", will have the authentic undefined value inside the function.
Why would undefined be overwritten? Who knows. People who write code for pages that also have to host 3rd-party code (generally adverts and things like that) become paranoid and defensive after a while.
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