Re:How I converted my React app to VanillaJS
by tomcam on Monday January 02, 2017 @06:10AM
(#13300474)
Can someone explain this interesting snippet?
[ Parent
| on HN ]
Re:How I converted my React app to VanillaJS
by teniutza on Monday January 02, 2017 @06:30AM
(#13300546)
It Uses "fat arrow" functions (lambdas) to define function. It is "equivalent" (there are several differences here, like "const" and the context of the function) to:
var h1 = function() {
return makeElement(`h1`);
}
Edited to reflect the valid point brought up in the comments
Re:How I converted my React app to VanillaJS
by drdaeman on Monday January 02, 2017 @07:28AM
(#13300767)
Not really an equivalent, because "this" would be different - unlike with function(){}, there is no automatic this-context binding with arrows, so just switching to a fancier syntax in existing code may break things.
Also, no "arguments" (there are ...rest parameters that allow for variadic functions).
Re:How I converted my React app to VanillaJS
by divbit on Monday January 02, 2017 @11:25AM
(#13302034)
> no automatic this-context binding with arrows,
I thought the arrows were now binding to surrounding context? (e.g. https://babeljs.io/blog/2015/06/07/react-on-es6-plus)
Re:How I converted my React app to VanillaJS
by nicoburns on Monday January 02, 2017 @11:30AM
(#13302084)
It is equivalent in this case though, as the function body doesn't use any of those features...
Re:How I converted my React app to VanillaJS
by closetnerd on Monday January 02, 2017 @06:56AM
(#13300627)
Techinically you're missing a return.
Re:How I converted my React app to VanillaJS
by tomcam on Monday January 02, 2017 @06:42AM
(#13300585)
Ah, thanks a ton. Everything I looked up was too general to help!
before you have to write
makeElement(`h1`)
everywhereafter this declaration you can write
h1()
insteadjust a shortcut function
Re:How I converted my React app to VanillaJS
by tomcam on Monday January 02, 2017 @06:30AM
(#13300545)
Sorry, 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.Yes, 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 {}
Re:How I converted my React app to VanillaJS
by pluma on Monday January 02, 2017 @07:17AM
(#13300722)
To 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.
Re:How I converted my React app to VanillaJS
by teniutza on Monday January 02, 2017 @06:37AM
(#13300568)
Yes, 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.
Re:How I converted my React app to VanillaJS
by tomcam on Monday January 02, 2017 @06:43AM
(#13300590)
OK, thanks. So it is the parameter list, not the return value
Re:How I converted my React app to VanillaJS
by mattnewton on Monday January 02, 2017 @10:16AM
(#13301542)
This is declaring a constant and assigning a function that takes no arguments to that constant.