Chapter 3, Starting Out with Functions – A Core Concept
3.1 Uninitialized object? The key is that we didn’t wrap the returned object in parentheses, so JavaScript thinks the braces enclose the code to be executed. In this case, type
is considered to be labeling a statement, which doesn’t really do anything: it’s a (t)
expression that isn’t used. Due to this, the code is considered valid, and since it doesn’t have an explicit return
statement, the implicit returned value is undefined
.
The corrected code is as follows:
const simpleAction = (t:string) => ({ type: t; });
See developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/label for more on labels, and developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions#Returning_object_literals for more on returning objects.
3.2 Are arrows allowed? There would be no problems with useArguments2()
, but with useArguments()
, you would get...