3 ms·
before you have to write makeElement(`h1`) everywhere after this declaration you can write h1() instead just a shortcut function
by k__ 10y ago
before you have to write
makeElement(`h1`)
everywhere
after this declaration you can write
h1()
instead
just a shortcut function
- tomcam 10y agoSorry, it was the syntax up to the makeElement(`h1`); that I didn't understand: const h1 = () => Is this new syntax declaring a function named h1 that returns a function or what? I don't understand what the empty pair of parentheses means.
- k__ 10y agoYes, this is a shortcut for: const h1 = function() { return makeElement('h1'); } Arrow functions without arguments need empty paranthesis. They also have an implicit return if you don't use {}
- teniutza 10y agoYes, it's ES6/ES2015. It's a lambda function ("fat arrow notation"). In the parentheses you would specify the arguments. An empty pair of parentheses means that the function does not expect any parameters.
- tomcam 10y agoOK, thanks. So it is the parameter list, not the return value
- pluma 10y agoTo add to the other explanations: it's also important to point out that arrow functions are slightly different from regular functions in that they don't redefine `arguments` and `this` so these keywords behave like any other variable would in a closure. So it's not just a syntactic shorthand but there's also a semantic difference.
- mattnewton 10y agoThis is declaring a constant and assigning a function that takes no arguments to that constant.