Introduction
You must have seeing people abusing and hating JS, because they compare Javascript with other languages like Java, C++, Go but Javascript is entirely different.
In this post I will be showing some cool things in javascript.
Generator Function ⚡
These are a type of function which can pause and resume its execution.
In simple words, suppose you call this function and you want to pause it execution at a certain state/condition and after certain condition you want to again resume its execution, you can do using generator function.
In the above example, you can see generator function are created using Asterisk(*) after writing function and when you want to pause its execution use yield and to stop use return, you can even return values using yield.
If you want to resume the execution, execute .run() method of the generator object.
Usages
- Iterator
- Generating infinite number efficiently
Read More (Ctrl + Click)
Some More (Ctrl + Click)
Async Await vs Generator Function ⏳
Generator Functions and Async Functions can be used to write asynchronous code which can wait.
Generator function always yields an object like {value: any, done: bool} but Async function returns a promise to resolve or can throw an error if doesn't resolves.
Generator function runs till yield and pauses but Async function runs till await and waits there.
Closure 🤏
Closure is an environment, created inside a function which stores some variables and can be used by another function inside that scope.
In the above example, you can see how the parentName is bind with child function.
Usages
- Hiding data inside function.
- Maintaining state.
Currying 🍛
Suppose of you have a function with N arguments, converting it into N function calls with only 1 arguments, is called Currying in Javascript.
Famous Question: Create a currying function to give sum of N numbers e.g. function(1)(2)(3).....(N) = 1+2+3+...+N
Usage
- Used to create Higher Order Function
- Memoization
- Error handling
- Initializing functions
Higher Order Functions (HOF) 💪
HOF accepts functions as argument and/or returns function with closure.
E.g. Array methods like map, reduce, filter.....etc.
Usage
- Binding functions with state
Call, Apply & Bind 📞
Call, Apply and Bind are JS methods using to bind object with this.
In the above example, I have shown how you can use call, apply and bind.
Usage
- DRY: Do Not Repeat Code
- Debouncing
Connect Me @ Linkedin, Github, Twitter, Youtube 😇
Thanks to Akshay Saini for his amazing series on JS.
Top comments (31)
Posts such as these are targeted at beginners. The concepts and alternatives you suggest are not just your own opinions regarding what is good and bad but also makes things unnecessarily complex.
Yes, you're very smart but not everyone has a decade of experience under their belt. Those who have, aren't going to come and read these posts.
I fail to see how anything he mentioned makes things unnecessarily complex.
That's the problem with "senior" developers on dev.to. They fail to see things from a beginner's perspective. If you know these concepts then this post isn't for you.
Really nice clickbait. 👌
I'm not a senior developer but medior. Maybe I don't see it well from the beginner perspective - that's what I asked the question and that's why I was expecting a good answer.
Honestly I don't see why people find arrow methods so confusing. I find them much clearer and easier to read.
The first example implements an iterator interface along with recursive. The watered down version is cute but multi-level arrow functions and currying, do you think a beginner would find that easy?
I hope you found this answer good enough. If you still don't see how people find this difficult perhaps you could sit down with a beginner or better yet, spend some time teaching JavaScript to someone as their first programming language, as in actually mentor someone.
You'll be very frustrated.
I didn't click on this when its previous title, so, I won't concern myself with that. If we are discussing this, let's not casually ignore the iterator interface and recursion. Currying also causes additional complexity because you're throwing functional programming into the mix.
Regardless of what I say, you'll remain firm in your point of view and speak from your experience as if it is the experience of every new developer. Anyways, I don't want to talk about this further.
I prefer your answers but I have a very grave problem with this attitude where senior developers shove their opinions down other's throat. Even I haven't really studied functional programming, what are the odds that a beginner would've.
Have a good day Luke!
Cheers
Thanks for the feedback, renamed the title.
Yes, I avoided arrow function in currying because its quite easy to understand returning functions for beginners.
Almost every thing in JS can be written on our own, it doesn't need to know the original javascript concept.
Cheers
Of course, you're allowed to give feedback. I hope I didn't cause any strife, if I did, I'm sorry. Regarding seniorities, well, that's something we can agree on. Thanks. :)
Currying is a hard concept on it's own for beginners. Arrow functions don't make it much harder in my opinion - it's just a different syntax.
... yeah, whatever.
Insightful post for beginners! Well done! Some advice: do make sure you use the tag #beginners or at least set the post's expert level down to a lower level since it is showing up in timelines of people that are not so which has caused this mess in the comment section.
Thanks for the feedback, will update it.
Comparing Generator function with eval is vague.
I would use generator function instead of creating my own generator object then use it.
Its an open world, not always everyone has to agree with everyone. We all have our opinion. You are free to share your opinion neither I am hiding any negative comments.
You are also free to report this post.