const buildUrl = (version, path) => `api/${version}/${path}`;
what does this do? How does this work? what is that =>
const buildUrl = (version, path) => `api/${version}/${path}`;
what does this do? How does this work? what is that =>
The => is whats called an arrow function, read more here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions
These two are the same with a few small differences, that doesn't come into effect here:
(version, path) => `api/${version}/${path}`
function(version, path) {
return `api/${version}/${path}`
}
if the backticks (``) in the string are used to create template string,
so they can a string and place variables (version, path) into them as denoted by the ${}
.
`api/${version}/${path}`
If the version is 1.0.0
and path is home
, then the string template will return
api/1.0.0/home
Read more about template strings here:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals
The lamda => causes your buildUrl constant to turn into a function that can be called like any other function where "version" and "path" become parameters of your function.
buildUrl("test1","test2")
… would simply return "api/test1/test2"
Surrounding your string with the back ticks "`" will cause the string to be interpolated which causes each variable contained in ${somevar} to be evaluated or rendered.