Back to articles
Angular & the "Disappearing this": Why Arrow Functions are More Than Just Syntax Sugar

Angular & the "Disappearing this": Why Arrow Functions are More Than Just Syntax Sugar

via Dev.toArkadiusz Graczyk

In the Angular world, we use arrow functions () => {} almost instinctively. Whether it's inside a .subscribe() , a setTimeout() , or an RxJS operator, the "arrow" is our default tool. But here's a fun question: why do we almost never see the function keyword inside a component class? It's not just about writing less code. It's about solving one of JavaScript's oldest puzzles: The Execution Context . "Let's get back to the basics — not because they're simple, but because they're hiding in plain sight." Chances are you've written () => hundreds of times this week alone. Inside a .subscribe() , a .pipe() , a .map() . It just works, so you move on. But the moment you stop and ask "wait, why does this actually work?" — that's when things get interesting. Let's take a closer look at something you already know, but might have never really seen . When this Forgets Who It Is In traditional JavaScript, the value of this is dynamic . It depends on how a function is called, not where it was define

Continue reading on Dev.to

Opens in a new tab

Read Full Article
1 views

Related Articles