JavaScript Bind in 5 Minutes




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”

  1. 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.

  2. 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?

  3. 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?

  4. 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?

Leave a Reply