As I noticed they didn’t explain the difference between () => {}
and function() {}
, then although there is an answer marked as correct, I will explain this difference.
First of all it is very common to think that both codes are equivalent, since the ES6
brought several syntax sugar
to make the code more readable and concise, the arrow functions
are usually confused with functions
of ES5
. But getting to the point there are five differences between both codes:
Context
Arrow functions
possess this léxico
while the normal model has this dinâmico
. It means that arrow functions
inherit the local context from which it was declared, while the normal model has the context associated with the object it is linked to at the time of the call (if it is not associated with anyone in the call, it will assume this
automatically as the global context, which in the case of browsers is window
)
var normal = function() {
return this === obj;
};
var arrow = () => {
return this === window;
};
var obj = { normal: normal, arrow: arrow };
obj.normal(); // true
obj.arrow(); // true
normal(); // false
Constructor
Arrow functions
cannot be constructors
, then it is not possible to use the operator new
with the same.
var Normal = function() {};
var Arrow = () => {};
new Normal(); // Normal {}
new Arrow(); // Arrow is not a constructor
Arguments
Arrow functions
does not have the array-like object arguments
.
var noop = () => {
return arguments;
}
noop(); // ReferenceError: arguments is not defined
Function name
Function expressions can be named explicatively, this is useful in some scenarios involving recursion and for in cases of exception it is easier to track code, since the function name is used in the exception stack shown to the developer. Only that Arrows Functions
cannot be named explicitly, they end up inheriting the name of the variable where it was created.
var fn = function nome() {};
fn.name; // nome
var fn = () => {};
fn.name; // fn
Return
Function expressions need to explicitly state what the function return will be, while Arrow Functions
allow writing in a shortened template where the last analyzed expression will be the return of the function when keys are omitted {}
.
var fn = function() { return 1; }; // retorna 1
var fn = () => 1; // retorna 1
var fn = () => (1, 2, 3); // retorna 3, última expressão avaliada
var fn = () => { 1 }; // retorna undefined
var fn = () => { return 1 }; // retorna 1
What would be an equivalent model of () => {}
then?
Ignoring the case that arrow functions
cannot be used as constructors
and do not receive arguments
, the most equivalent model between arrow functions
and traditional functions would be this:
// modelo nomeado
var name = (() => { /* code */ })
var name = (function name() { /* code */ }).bind(this)
// modelo anônimo
(() => { /* code */ })
(function() { /* code */ }).bind(this)
In this case the code
can be exactly the same between the two models and they will work exactly the same. But of course, there are other ways to simulate the behavior of arrow functions
. One of them is to store the context this
in a variable and use that variable in the traditional function instead of its own this
, what the other answers showed.
Any error appears in the console, the request is made?
– DiegoAugusto
the request is made yes. When I give a
console.log(response.data)
it returns me the right product array. But an error appears :TypeError: Cannot set property 'products' of undefined
– Rafael Miller
Tries to initialize the product list,
products = [];
in the overall scope– DiegoAugusto