var myFunc = function(x = getUndefined()){
return x
};
function getUndefined(){
return undefined
};
I want to know why this returns undefined instead of throwing some sort of error. How does x know not to keep calling getUndefined()
?
To get a sense of what is happening under the hood, you can look at how this code transpiles down to ES5:
"use strict";
var myFunc = function myFunc() {
var x = arguments.length <= 0 || arguments[0] === undefined ? getUndefined() : arguments[0];
return x;
};
function getUndefined() {
return undefined;
};
The function getUndefined
is only called if there is no passed value for x
.
While ES6 engines might not follow this exact strategy, it is most likely fairly similar.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.