1

I saw examples of 2 types of bind calls. first kind is:

    const book = {
      title: 'Brave New World',
      author: 'Aldous Huxley',
    }

    function summary() {
      console.log(`${this.title} was written by ${this.author}.`)
    }

    const braveNewWorldSummary = summary.bind(book)

now "this" in summary is now bond to the "book" object

the second kind is:

  function utilizationUpdate(chart, data) {....}

  function utilizationInit(component, btnGroup, data){
        ....
        var myChart = new Chart(ctx, {....}
        $(btnGroup).on("change", function() {
                    ...
                    fetch('{% url 'api_utilization' fac.fac_id %}?measure=' + measure + '&{{ request.GET.urlencode() }}' )
                    .then(status)
                    .then(json)
                    .then(utilizationUpdate.bind(this, myChart));
                }
            });
        }

"chart" in utilizationUpdate() is now bond to myChart.

Why in first bind() example, "this" is not an argument to be passed? but it is specified in the second bind() example?

2
  • 1
    first param is this, subsequent are params, have a ponder at the docs. Your first example is not the same as second, in the second example, myChart is passed as first param chart in utilizationUpdate Commented May 13, 2021 at 21:08
  • Thanks. Got it now. Commented May 13, 2021 at 21:16

1 Answer 1

1

The first argument passed to bind is the this value and the other arguments are the additional arguments to the function. In the first case, it only sets the this value, while in the second case, it sets both the this value and the first argument. However, it is likely that the this value wasn't truly needed, so null could have been passed too.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.