Lädt...


🔧 7 Differences Between Arrow Functions and Traditional Functions


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

The arrow function that appeared in ES6 has brought us great convenience, but it is still different from the traditional function. A little carelessness in development may cause problems, so follow me to see the 7 differences between arrow function and traditional function!

1. No arguments

In a traditional function, there is an arguments local variable. If the number of arguments to our function is dynamic, using arguments allows us to easily do things like calculating the maximum number of passed arguments:

arguments is an array-like object. The similarities between array-like objects and array objects are that they can both use subscripts to access corresponding elements and have a length property. The difference is that array-like objects do not have built-in methods on arrays, but we can use Array.from converts an array-like object into an array object.

In the arrow function, there are no arguments, if we access arguments in the arrow function will return the arguments of the closest non-arrow parent function.

But we can use rest parameters instead, which get a normal array object.

2. Return value can omit the curly brackets

You can see that in an inline arrow function that contains only one expression, we can omit the curly braces to return the value, which makes the code clearer.

3. Duplicate named parameters are not allowed

In non-restrict mode, traditional functions allow us to use duplicate named parameters. But in strict mode, it is not allowed.

In arrow functions, parameters with the same name are not allowed whether the strict mode is enabled or not.

4. No prototype

We can get prototype for traditional function, but the arrow function does not have prototype .

5. No this

In a traditional function, its internal this value is dynamic , it depends on how the function is invoked. For example:

In the arrow function, there are no this, if we access this in the arrow function it will return the this of the closest non-arrow parent function.

Note that the this of an arrow function is determined at the time of declaration and never changes. So call, apply, bind cannot change the value of arrow function this.

6. Cannot be invoked with new

We can use the new keyword on the traditional function to create a new object.

But arrow functions cannot be called with new.

This is because when calling new, we go through the following four steps:

  1. Create a new object

  2. Point the __proto__ of the new object to the prototype of the constructor

  3. Call the constructor with the new object as this

  4. If the result of the call is an object, return the object, if not, return the new object created in the first step.

We can implement a mock new function:

So you can see that the arrow function cannot be called by the new keyword because it has no prototype and no this.

7. Cannot be used as a Generator function

For historical reasons, the specification does not allow the use of the yield command in the arrow function, so the arrow function cannot be used as a Generator function.

To sum up, arrow functions avoid a lot of code scenarios that can lead to misunderstandings, it makes our code clearer and more controllable.

If you find this helpful, please consider subscribing to my newsletter for more insights on web development. Thank you for reading!

...

🔧 Draft: What are the differences between arrow functions and traditional functions?


📈 71.94 Punkte
🔧 Programmierung

🔧 7 Differences Between Arrow Functions and Traditional Functions


📈 71.94 Punkte
🔧 Programmierung

🔧 The difference between Arrow functions and Normal functions in JavaScript


📈 43.21 Punkte
🔧 Programmierung

🔧 Pratical Differences between GCP Cloud Functions and AWS Lambda Functions


📈 41.87 Punkte
🔧 Programmierung

🔧 Differences between arrow function and regular function in JavaScript


📈 39.15 Punkte
🔧 Programmierung

🔧 JavaScript Regular/Normal vs Arrow Function: My Beef with Arrow Functions.


📈 38.54 Punkte
🔧 Programmierung

🔧 Why the "this" Keyword Behaves Differently in Regular Functions and Arrow Functions


📈 34.13 Punkte
🔧 Programmierung

🔧 Arrow Functions vs. Regular Functions in JavaScript: A Showdown


📈 32.46 Punkte
🔧 Programmierung

🔧 Mastering JavaScript Functions: Your Guide to Normal vs. Arrow Functions


📈 32.46 Punkte
🔧 Programmierung

🔧 JavaScript Arrow Functions vs Regular Functions


📈 32.46 Punkte
🔧 Programmierung

🔧 Arrow Functions vs. Regular Functions in JavaScript: A Comprehensive Guide


📈 32.46 Punkte
🔧 Programmierung

🔧 Understanding Arrow Functions vs. Normal Functions in JavaScript


📈 32.46 Punkte
🔧 Programmierung

🔧 Differences between Ethereum’s send/transfer/call functions


📈 31.4 Punkte
🔧 Programmierung

🔧 DevOps vs. Traditional: Key Differences in Software Development and Delivery


📈 30.4 Punkte
🔧 Programmierung

📰 Arrow Forum 2024: Arrow feiert ein großes Familienfest - channelpartner.de


📈 29.74 Punkte
📰 IT Security Nachrichten

📰 Channel-Treff in München: Arrow veranstaltet Arrow University - channelpartner.de


📈 29.74 Punkte
📰 IT Security Nachrichten

🕵️ arrow-kt Arrow up to 0.8.x Gradle Build Artifact Resolver weak encryption


📈 29.74 Punkte
🕵️ Sicherheitslücken

📰 Arrow ECS lädt zur »Arrow University«


📈 29.74 Punkte
📰 IT Security Nachrichten

🔧 Functions of Commercial Bank: Primary Functions and Secondary Functions


📈 28.05 Punkte
🔧 Programmierung

📰 Honeycomb Metrics bridges the gap between traditional debugging practices and modern systems


📈 25.95 Punkte
📰 IT Security Nachrichten

📰 Jibrel Network Is Bridging The Gap Between Crypto And Traditional Markets


📈 25.95 Punkte
📰 IT Security Nachrichten

🐧 What are the differences between Nix and Guix? Which one do you prefer and why?


📈 25.95 Punkte
🐧 Linux Tipps

🔧 Differences Between SQL and NoSQL Databases and Their Combined Use in Projects


📈 25.95 Punkte
🔧 Programmierung

🔧 Angular Addicts #25: Angular and Wiz will be merged, the differences between React and Angular & more


📈 25.95 Punkte
🔧 Programmierung

🐧 Differences between free, open source software and paid closed source software becoming lesser and lesser.


📈 25.95 Punkte
🐧 Linux Tipps

🕵️ Similarities and differences between MuddyWater and APT34


📈 25.95 Punkte
🕵️ Hacking

🐧 What are the differences between Nix and Guix? Which one do you prefer and why?


📈 25.95 Punkte
🐧 Linux Tipps

matomo