what is bind() method in javascript?

Discussion in 'Web Design and Development' started by sarahjoness, Sep 11, 2015.

  1. sarahjoness macrumors newbie

    Joined:
    Dec 15, 2014
    Location:
    California
  2. ghellquist macrumors regular

    Joined:
    Aug 21, 2011
    Location:
    Stockholm Sweden
    #2
  3. NutsNGum macrumors 68030

    NutsNGum

    Joined:
    Jul 30, 2010
    Location:
    Glasgow, Scotland
    #3
    The advice is don't use bind() in jQuery these days. Use the on() method instead.
     
  4. iPaintCode, Oct 7, 2015
    Last edited: Oct 12, 2015

    iPaintCode macrumors regular

    iPaintCode

    Joined:
    Jun 24, 2012
    Location:
    Metro Detroit
    #5
    He/She asked about the bind method in JavaScript not jQuery, there's a big difference between the bind method in JavaScript and bind in jQuery. Far as jQuery the bind method is completely different and is used to bind (attach) a handler to an eventType for an element, like a click event or custom event handler that you can trigger later. The problem with bind in jQuery is that removing the element doesn't remove the bound event and this leads to memory leaks, especially when you're adding and removing dom elements. This is why on is the preferred choice due to event bubbling / delegation. If you set the event on a parent dom element, say an unordered list and it's going to check first what child element was clicked (list element), but the benefit is when removing child elements they don't stay bound to that event because the event is attached to the parent.

    Far as JavaScript bind() goes, it's more so to do with "the context" and scope of the this keyword and functions, I'd suggest looking up apply() and call() as well, they're somewhat related to bind. Though bind works differently than apply and call as it creates a new function and sets the context but doesn't execute the new function, though you can add (), invocation, to immediately invoke bind but if that's the case you're better off using call or apply (if you need to pass in an array of arguments). In a nutshell bind will allow you to create a new function of a function and set its context to the function you copied but invoke it at a later time. Where as call and apply are used to change the context of this to something else when invoking a function.
     
  5. NutsNGum, Oct 7, 2015
    Last edited: Oct 7, 2015

    NutsNGum macrumors 68030

    NutsNGum

    Joined:
    Jul 30, 2010
    Location:
    Glasgow, Scotland
    #6
    Whoopsy doodle.

    Although I would hazard a guess that it's probably a she -- sarahjoness
     
  6. webiletechnologies macrumors newbie

    webiletechnologies

    Joined:
    Jul 12, 2016
    Location:
    Ahmedabad
    #7
    bind in javascript is a method -- Function.prototype.bind . bind is a method. It is called on function prototype. This method creates a function whose body is similar to the function on which it is called but the 'this' refers to the first parameter passed to the bind method.
     

Share This Page