I think it's worth pointing out that you can bind any or all of the arguments, not just 'this'.
e.g.
const context = {}; function setName(name) { this.name = name; } const setContextNameToFred = setName.bind(context, 'fred'); setContextNameToFred(); console.log(context); // { name: "fred" }
And that it also works where 'this' doesn't.
const add = (a, b) => a + b; const addOne = add.bind(null, 1); console.log(addOne(2)); // 3
Likewise for apply and call.
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I think it's worth pointing out that you can bind any or all of the arguments, not just 'this'.
e.g.
And that it also works where 'this' doesn't.
Likewise for apply and call.