Search code examples
javascriptbindingthispass-by-referencepass-by-value

Does .bind(this) pass by reference or by value?


I create a function somewhere and I bind it to this so that I can use the parent block's meaning of this as the value of this within the function. For example:

var foo = function() {
    // some stuff involving other stuff
}.bind(this);

Is the this I pass as an argument to bind passed by reference, or by value? So if I change the parameters of the this object a bit later in the outer block of code, and afterwards call foo, will foo use the value of this at the time I called bind, or at the time I called foo?


Solution

  • So if I change the parameters of the this object a bit later in the outer block of code, and afterwards call foo, will foo use the value of this at the time I called bind, or at the time I called foo?

    at the time you called foo.

    this is a reference to Object. That means Object may get mutated at some point and you will get "fresh - up to date" values of it.