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
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With