Sobes.tech
Middle+

If several contexts are bound sequentially, which context will be in the end?

sobes.tech AI

Answer from AI

If multiple contexts are sequentially bound (for example, in Node.js using Function.prototype.bind), the final context will be the one passed in the last call to bind.

Each call to bind creates a new function with a fixed this value. If bind is called again on an already bound function, the new context will overwrite the previous one.

Example:

function show() {
  console.log(this.name);
}

const obj1 = { name: 'Obj1' };
const obj2 = { name: 'Obj2' };

const bound1 = show.bind(obj1);
const bound2 = bound1.bind(obj2);

bound2(); // Will output 'Obj1', because bind does not change context when called again

However, it is important to note that re-binding a function with bind does not change the context — the first bind permanently fixes the context. If you are talking about other mechanisms of context binding (e.g., in libraries with nested contexts), usually the last applied context takes precedence.

If several contexts are bound sequentially, which… - sobes.tech