+ 5
Ore The context (😉) of "context" in your explanation below is a bit unclear:
"Context refers to the object which
'owns' the lexical scope in which a function is called."
Are you referring to traditional functions, arrow functions, or both?
Regarding this other part of your explanation:
"Arrow functions do not bind (cannot be bound) to a different context when called but they reside in the current execution context."
It's unclear what you are referring to by "_current_ execution context" since "_current_" is relative and therefore inherently changeable. 😉
Did you mean to say something to the effect of, "the _original_ execution context from when the arrow function was created"?
Also, you mentioned your "code shows the difference". However, that code highlights an exception to what many people would have expected due to missing a very subtle characteristic about that code that would explain the output.
+ 3
That's syntax for arrow functions. They are typically anonymous functions..
function(foo){
return bar
}
(foo)=>{
return bar
}
Both ways are valid.
+ 3
=> is used after ECMA SCRIPT 5
It is used to define functions.
Before we used
function foo() {
// some code
}
Now we use
const foo = () => {
// some code
}
Both are same
+ 3
They aren't anonymous function ,to define a anonymous function we use IIFE ,
arrow functions (=>) are a concise way of declaring function ,
Instead of ,
function foo(){
return bar
}
using arrow functions it can be written as foo=()=>bar;
You don't even need a return and curly brackets here if it's the only value that's going to be returned or logged to the console ,if I remember correctly they don't have the "this" object as well which points to function name but in arrow functions it rather points to window object ,I might be wrong on the "this" .
But this isn't something that's rare or a new feature ,you could have google it just ,there are many more comprehensive tuts on what it is instead of depending on us explaining you ,
+ 2
There are good answers already. I just want to add that arrow functions are slightly different from normal functions. You need to understand context to understand the difference.
Context refers to the object which "owns" the lexical scope in which a function is called. The context is determined by a variety of factors and can be accessed with the this keyword.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this
Arrow functions do not bind (cannot be bound) to a different context when called but they reside in the current execution context.
This code shows the difference
https://code.sololearn.com/W3RXdD6P3UPd/?ref=app
That reminds me of a meme I came across some months ago.
https://www.sololearn.com/post/463183/
There are other differences too e.g arrow functions cannot be constructors or generators.
+ 2
Ore thank you for correcting and clearing up few things for me as well ,i was thinking that since in the following code:
const F1=function (){
}
We can call F1 many times like any normal function ,
So it wudn't behave like a lambda like we have in python and IIFE works exactly when compared to that .But seems like those unnamed function also comes under anonymous ones that I never tried to understand before , Again ty ,I really appreciate all the info you added .
+ 1
Abhay In my understanding, anonymous functions(or lambdas) are functions without a name. They are usually assigned to variables or passed as callbacks
const f1 = function(arg) {
return arg ** 2;
}
const f2 = arg => arg **2;
! function(arg) {
return arg ** 2;
} (4);
f1, f2 and the IIFE are all anonymous functions aka lambdas.
+ 1
David Carroll
My definition is not very good. Context is not an object. It is more like a lexical environment with a new scope, 'this' object etc.
But I believe the entire code is seperated into many execution contexts. Each traditional function runs on a distinct context. Arrow functions don't. Arrow functions are considered a part of the execution context they are defined.
Case A.
If an arrow function is enclosed in a traditional function called fun,
function fun() {
() => {}
}
arguments = arguments object of fun
this = this object of fun
In short, it runs in the outer function's execution context. This is what I meant by "current execution context". Pardon my bad grammar.
Case B.
If the arrow function is not within a function. It uses Global Execution Context (GEC).
this = window
arguments = undefined
Case C.
If the arrow function is an eval string, it uses Eval Execution Context. Similar to GEC in many ways.
0
Abhay You are welcome 👌



