Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do arrow functions have a higher priority than normal functions?

Tags:

javascript

Both of the following snippets print "arrow". I would like to know why. Do arrow functions have a sort of higher priority than the normal functions if they both have the same name?

function increment(){
    alert("normal")
}


var increment = () => {
    alert("arrow")
}

increment(); //prints arrow
var increment = () => {
    alert("arrow")
}


function increment(){
    alert("normal")
}


increment(); //prints arrow
like image 320
Neon Avatar asked Nov 07 '19 03:11

Neon


People also ask

How are arrow functions better than normal functions?

Unlike regular functions, arrow functions do not have their own this . The value of this inside an arrow function remains the same throughout the lifecycle of the function and is always bound to the value of this in the closest non-arrow parent function.

Are arrow functions faster than regular functions?

Conclusion. Arrow functions and especially arrow functions using implicit returns do take more time to run compared to traditional functions. Implicit returns suffer from the same issues that arrow functions do, in that they take more compilation time.

Are arrow functions preferred?

Arrow functions shine best with anything that requires this to be bound to the context, and not the function itself. Despite the fact that they are anonymous, I also like using them with methods such as map and reduce , because I think it makes my code more readable.

How does the arrow function differ from other functions?

1.2 Arrow function The behavior of this inside of an arrow function differs considerably from the regular function's this behavior. The arrow function doesn't define its own execution context. No matter how or where being executed, this value inside of an arrow function always equals this value from the outer function.


1 Answers

This has nothing to do with arrow functions. Rather, regular functions (and var declarations) are hoisted; regardless of where you write them, they are moved to the top of their scope. Effectively, both code samples are completely identical, and they look like this:

var increment; // hoisted

function increment() { // hoisted
    alert("normal")
}

increment = () => { // the assignment itself is unaffected
    alert("arrow")
}

increment(); //prints arrow

The assignment portion of var increment = ... occur after the hoisted function and var declaration, in both cases. Regardless of where you actually wrote the function increment() { } declaration, it is hoisted above the line performing assignment to the increment variable.

This is why the following code still works, despite the function apparently being defined after it is used:

increment(); //prints normal

function increment(){
    console.log("normal")
}

If you want to compare like with like, you need to compare var increment = () => { ... } with var increment = function () { ... }, that is, two assignments. The results look like this:

var increment = () => { console.log('arrow'); }
var increment = function () { console.log('normal'); }

increment(); # normal

vs

var increment = function () { console.log('normal'); }
var increment = () => { console.log('arrow'); }

increment(); # arrow

In both cases there is a single hoisted var increment; declaration, and then the assignments occur in the order in which they're written, meaning the last assignment wins.


As an aside, this is one of the chief reasons to prefer let x = () => { } over "old" style function declarations. let is not hoisted, so the function exists from the point in time you would naturally expect it to, instead of jumping to the top of your scope.

like image 57
meagar Avatar answered Sep 28 '22 13:09

meagar