-
-
Save getify/319e64fe362eb8c2132902c1794c14ae to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Doesn't throw an error, but doesn't return an object | |
const getObj = () => { a: 1 }; | |
console.log(getObj()); //=> val 'undefined' : void | |
// Reason: It looks like an arrow function returning an object, but it isn't. | |
// It's an arrow function with a function body (containing a labeled | |
// statement) that returns nothing. | |
const getObj = () => | |
{ // <- Start function body | |
a: // <- A label to use when break/continue; e.g. break a; | |
1 // <- Just a noop expression-statement, not doing anything. | |
// <- No return, meaning it implicitly returns `undefined`. | |
}; // <- End function body | |
// Note: Function body looks like a block statement, but it's not the same. A | |
// block statement would have returned the `1` value, but regular function | |
// bodies do not have implicit return. | |
// To actually return an object, turn into an expression instead of function body | |
const getObj = () => ({ a: 1 }); | |
console.log(getObj()); | |
//=> val { a : 1 } : { a: int } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment