JavaScript Why is .bind() used in this example? what is bond used for,bind used as a noun

var TwitterListModel = function (lists, selectedList) {
    // [...]

    this.addUser = function () {
        if (this.userNameToAdd() && this.userNameToAddIsValid()) {
            this.editingList.userNames.push(this.userNameToAdd());
            this.userNameToAdd("");
        }
    };

    this.removeUser = function (userName) {
        this.editingList.userNames.remove(userName)
    }.bind(this);

    // [...]
}

I've found this code in this page from Knockout JS examples. They are declared inside of an object. I'd like to understand why the first function doesn't use .bind(this) and the second does.

When and why is it necessary to use .bind(this)? It looks like if I use it or not makes no difference: the meaning of this is always referred to the object in which the method is declared (and not the anonymous function in which it is found). Am I right?

Both these methods access this.editingList and they're referring to the same variable in both cases.

Answer:1

removeUser is bound to the context of the row in the array.

$root points out the parent, but when the click handler is called the this context will be pointing to the item clicked. This is by design by the KO team.

You can also solve it like this

<button data-bind='click: $root.removeUser.bind($parent)'>Remove</button>

This will also work with protoype function declaration

Answer:2

You can play with the fiddle they provide for this example...

If you try to remove the .bind(this) you will see that this (add a console.log(this) in this method) then refers to the username. This is due to the fact that the removeUser function is bind on the button in the loop over the usernames (<ul data-bind='foreach: userNames'>).

For the other binding, it is attached to the form and then goes with the whole model.

Answer:3

I would like to know if there is a way to modify an html item (i.e. a div) when a radio button is selected without using js. Here is the markup: <div class="container"> <input id="...

I would like to know if there is a way to modify an html item (i.e. a div) when a radio button is selected without using js. Here is the markup: <div class="container"> <input id="...

  1. highlighting html element

I was trying the following: f.addEventListener('submit',(function(frm){ var func = (function(e){somefunction(e,frm);})(e); })(f),false); But this is failing. I want to pass the form (f) as a static ...

I was trying the following: f.addEventListener('submit',(function(frm){ var func = (function(e){somefunction(e,frm);})(e); })(f),false); But this is failing. I want to pass the form (f) as a static ...

  1. javascript passing event as parameter
  2. javascript passing event to function
  3. javascript passing event
  4. javascript passing event onclick
  5. javascript onclick event passing parameters
  6. javascript passing parameters to event handlers
  7. javascript passing this to event handler
  8. javascript passing arguments to event handlers

so, I came across a bug and thought it was interesting. Once I sat and thought about it for 5 seconds, it made sense but curious how one would get past it in the future. so, I have some hashes set up ...

so, I came across a bug and thought it was interesting. Once I sat and thought about it for 5 seconds, it made sense but curious how one would get past it in the future. so, I have some hashes set up ...

i have hiddentfield whose value is changing on javascript. I just wanted to fire serverside event valuechanged event of hiddenfield when its value changed from javascript. I tried with : ...

i have hiddentfield whose value is changing on javascript. I just wanted to fire serverside event valuechanged event of hiddenfield when its value changed from javascript. I tried with : ...

  1. fire server side event from javascript asp.net
  2. fire server side event from jquery
  3. fire server side event from javascript
  4. how to fire server side event for html checkbox
  5. fire server side click event from javascript
  6. how to fire server side event on click of the button
  7. event doesn't fire server side