I've been looking for a good explanation about the bind and function execution for quite some time, and I found it today on my favorite youtube channel. Thanks, Max. =)
Seriously... I haven't had listened to any tutor so far that can explain things this well. No wonder people call him web-guru. This video is all I needed to understand this concept.
Yesterday I finally understood why we need bind() and today I successfully used it for the first time myself with complete understanding what is going on. Thank you Max. You are a JS ninja
Holy man! What a great explanation is such simple terms. I've read a few snippets on this topic and haven't found anything even close. This covers the "why" and "when" to use. Thnaks for sharing.
Max, I had just finished the .bind() section of your JS course and was still feeling a bit confused on when to use it. I spent a day or so reading through the MDN doc, but not making much headway. Suddenly today, I see this video, and your explanation here really helped to cement the concept in my head! Thank you!
Gerade heute in meine Notizen mit aufgenommen: "bind() recherchieren" und schwups lieg ich abends auf der Couch und dein Upload kommt als Push-Nachricht 👍 Danke!
Great explanation! Maybe you should have mentioned the case when we don't use any arguments in bind and use an event.target inside function on which we called bind method?
You are the most finely detailed instructor I know. Your lessons are never boring it's like you got some super power. Any chance you will ever do an updated laravel course? Php seems interesting and a good way of getting into the industry
I've will hate, love, hate and love Max for the rest of my life: the toughest but most brilliant teacher ever. Just a note when Max returns the function it might be even more clear by returning the whole content of the setAsActiveHandler like so function setAsActiveHandler(goalId, event) { console.log(event); return function () { const selectedGoal = goals.find(g => g.id === goalId); activeGoalElement.textContent = selectedGoal.text; } } this way at the first pass from the init() function setAsActiveHandler will be saved as a ready to be executed function at the click event by the user.
Max my question is why we need an event listerner? I can modify the innerHtml with some like `Button Text`; and avoid to add event listerner to browser right? I buy your sveltejs course on udemy and it's very good!
Nicely explained! Just a question! Can we just pass the goal object directly as an arg in setAsActiveHandler instead of its id, avoiding the find loop? Or there is a downside?
This is a great explanation of bind. Even though you only mentioned it in passing, it finally helped me understand the issue with "this" inside a function, why you would need "bind", and why arrow functions are so great.
With typescript last I checked bind did unfortunately not have the best of typing, add to that the need to provide a this context and I pretty much always use new defined (arrow) functions.
Max, could you explain me, please. You used an anonymous function in an addEventListener like addEventListener('click', () => {calling another function}) - and to me it is a way clearly than using a method bind(). Particularly in case of referencing to 'this' object. In the lesson 8. Binding Class Method & Working with this of Chapter 10 - you implemented an expression addCartButton.addEventListener('click', this.addToCart.bind(this)); in the first time - I've learnt Java and Python.... but I don't remember subtleties like this one in those languages. My question is if both methods are completely interchangeable? Having watched your video I went to my code of the lesson and changed the line into addToCartButton.addEventListener('click', () => { this.addToCart() }); It works well as before. I don't see any differences so far. Thank you in advanced!
Awesome, thanks for the clarification ! Can I assume that the main purpose of anonymous function is to be a pointer to another function ? Or is it just one of their uses ? (Not sure if I made myself clear)