How is an arrow function performed?

-3

(() => {
  alert("Anonima auto ejecutable");
         
})();

var cualquiera = (() => alert("Tipo expresion"))();

How can I do it as a normal function?

Bone function(){}

since:

cualquierFuncion() => alert("Error");

It does not work

    
asked by Eduardo Sebastian 01.08.2017 в 17:36
source

3 answers

2

In your first example, you are enclosing the arrow function inside an anonymous function and executing it by passing the parameters to it, in your example you do not need to pass parameters because you do not need them, therefore you call it (tuFuncion) (yourParameters) .

Your second example is not executable because it is not well formed. If you want to save the anonymous function in a variable to later execute it, it would be the following:

var someFunction = () => alert("Error");
someFunction();

Or you could also do it online as your first example:

(() => alert("Error"))()

To convert an arrow function to a normal function you only have to translate the syntax:

var sumarNormalFunc = function (a, b) {
    return a + b;
}

sumarNormalFunc(5, 5);

var sumarArrowFunc = (a, b) => { 
    return a + b
};

sumarArrowFunc(5, 5);

Source: Link

    
answered by 01.08.2017 в 18:06
0

To convert an arrow function to a normal function, it would be like this:

var cualquiera = function() { alert("Tipo expression"); }
cualquiera();

And the simplest way to make an arrow function would be:

var cualquierFuncion = ()=> alert("Expression flecha");

cualquierFuncion()
    
answered by 01.08.2017 в 17:57
0

It is clear that if you only want to show a alert , it would be more to store the result in a variable. Since I would not return anything. (only for this example, in other scenarios you may need to assign the return value)

So to convert this function to a normal one and execute it at the same time, you would have to enclose it in parentheses and at the end make the call with ();

(function(){
  alert("Function cualquiera");
})();

/* Si no desea ejecutar al cargar , solo sería */
function funcionxy(){
  return alert("Function cualquiera");
}
/* Para luego hacer la llamada donde desee */

 /*funcionxy();*/
  

Do not forget to review a more detailed explanation on this Here

    
answered by 01.08.2017 в 18:04