Reputation: 75
I am exploring arrow functions, I came up with this basic example, I just need to understand how the follwing works
var getServer = () => {
if(document.getElementById('server')){
var serverHTML = document.getElementById('server');
serverHTML.innerHTML = "Server: " + window.location.host;
}
if(document.getElementById('serverxs')){
var serverHTMLxs = document.getElementById('serverxs');
serverHTMLxs.innerHTML = "Server: " + window.location.host;
}
};
The getServer = () =>
part is confusing how is empty brackets correct? Or am I wrong.
Any documentation or answers appreciated
Upvotes: 0
Views: 60
Reputation: 2321
You can "read" arrow functions like this
(par1, par2) => "value"
turns into
function(par1, par2){ return "value"; }
So:
() =>
turns into a function with no parameters passed to it. An exception to this is when one argument is passed to an arrow function, so:
param=>{ return value; }
// turns into
function(param){ return value; }
Upvotes: 3
Reputation: 16103
It's a function without parameters, as short/arrow function:
var getServer = () => { /* ... */ }
// matches
var getServer = function (){ /* ... */}
// Example:
var example = function (foo, bar){ return foo;}
// matches
var example = (foo, bar) => { return foo;}
There are some caveats though, which you might want to check.
Upvotes: 1