Closed Explosion-Scratch closed 3 years ago
That code would be equivalent to this:
let x = (() => {
if (foo()) { return f() }
else if (bar()) { return g() }
else { return h() }
})();
The difference bwtween do expressions and iife (other than the implicit "return") is that you can use control flow statements that affect the outer scope. For example:
function getName(str) {
const person = do {
let data = JSON.parse(str);
if (!data.hasName) return null;
data.person;
};
return person.name;
}
is equivalent to
function getName(str) {
let didReturn;
let returnValue;
const person = (() => {
let data = JSON.parse(str);
if (!data.hasName) {
didReturn = true;
returnValue = null;
return;
}
return data.person;
})();
if (didReturn) return returnValue;
return person.name;
}
The difference between do expressions and iife (other than the implicit "return") is that you can use control flow statements that affect the outer scope. For example:
function getName(str) { const person = do { let data = JSON.parse(str); if (!data.hasName) return null; data.person; }; return person.name; }
So in this function if
!data.hasName
null
would be returned to from thegenName()
function, right? Also variables in ado
statement act much like awith
statment in that the variables and such defined there are scoped to the parent scope, right? So this would work:function aFunction(){ do { let scoped = "yay"; } return scoped }
but this would not:
function aFunction(){ (() => { let scoped = "yay"; })() return scoped;//Error, scoped is not defined }
do
expressions act exactly as normal block statements.
So in this function if
!data.hasName
null
would be returned to from thegenName()
function, right?
Yes, exactly.
So this would work:
function aFunction(){ do { let scoped = "yay"; } return scoped }
No, similarly to how this doesn't work:
function aFunction(){
{
let scoped = "yay";
}
return scoped
}
However, this would work:
function aFunction(){
(do {
var scoped = "yay";
});
return scoped
}
exactly as this currently works in JS:
function aFunction(){
{
var scoped = "yay";
}
return scoped
}
Okay! Thanks!
In the README there is an example as such:
What would the value of
x
be?Would it act like a function with
return
(and if so why have this)? What would the real usage of this?