To learn more advanced Web Development techniques, sign up for my brand new course on TypeScript at Udemy. https://www.udemy.com/typescript/?couponCode=TYPESCRIPT_NOW
Bind is a super useful (and confusing) native feature of JavaScript.
Not so sure about bind? Let this 5 minute tutorial show you how it’s done!
Original source
28 responses to “JavaScript Bind in 5 Minutes”
Many many thanks ! I have seen this topic several times but never like your explanation ! 🙂
Thanks a lot!
Thank you very much.
Couldn't explain it more simple than this.
Much appreciated!
Thanks alot!
Thanks!
Thanks a lot.
correct me if I am wrong but, bind is giving me flexibility to create a generic function that does something regardless of its context. When we invoke bind and pass in a value, it changes the context and perform its function/operation on the context provided.
You talk fast – I have to keep pausing it to keep up 🙂
great example and well explained
Can I ask why you wouldn't just pass in the object as a parameter for showName? As in:
function showName(obj){
return obj.name;
}
Then call:
showName(a);
Maybe I'm not looking far enough into the application of bind. But why use bind instead of writing a more versatile function?
Please, don't use a word 'confusing'. It is not a confusing concept.
you are good at teaching things and making it click! I was losing my mind trying to learn this and apply it in refactoring of my older project thanks!
Stupid question, but how do you type a new line in the Chrome console? When I press ENTER it enters the command.
I'd tried many different books, videos, and websites, but none of them could show how bind works in a simple enough way. You've done that. Great job.Thanks.
So whatever I pass to the bind function, it replaces 'this' into that function?
very informative and straight forward ! Javascript is a powerful language but has been underestimated by programmers who do not know much about web development !
Very helpful!
Thanks! Can you please make "applly" and "call" in 5 minutes?
Thanks dude
thank you for this. .bind is something i just seem to forget in my coding and waste waaay to much time with weird errors
Nice.. thank you very much
so "bind" is like making the function you bind a method to the particular object you pass into it as an argument? because if I do something like a.showName = showName.bind(a) it is practically a method. Or am I confusing something?
Ctrl + L => to clear chrome devtools console log 🙂
Awesome lessons, thanks a lot.
I take it you're a GOT fan?
Very useful. Thanks!
I know that the "this" in following refer to window. So it will give an error.
Var a = { method : 'jump',
Call: this.method}
But why does the example you gave with this used inside a function return the Object? Can you help this out?
The confusion in large part stems from the fact that bind does two things in JavaScript: context binding and partial application. http://glebbahmutov.com/blog/binding-vs-partial-application/
Little typo: cmd-option-i opens devtools.