Function - to call or to bind?

#1

I know the documentation mentions to use .call when passing functions to a child component, but it seems like I can also use .bind, is there any reason why I shouldn’t use .bind? I have a particular use case where I’m generating the callback function and passing that to .bind now and if its not recommended, not sure how I would do it with .call.

i.e. (just to paint the picture I know the example is not how you would normally do this)
parent component

items = []
getCallback(index) {
  return (value) => {
    this.items[index] = value;
  }
}
<child-component callback.one-time="getCallback($index)" repeat.for="item of items"></child-component>

child component

@observable model;
@bindable callback;

modelChanged(new,old) {
  callback(new);
}
1 Like
#2

Bind will not provide the typically expected context by default, so this will not point to the instance of your VM. Aside from that you’re fine

1 Like
#3

I’m imagine that is only if you pass the function reference like callback.bind="doSomething". In the original example, callback.one-time="getCallback($index)"context is maintained and this.items is accessible.

1 Like