Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to unbind this from a function in javascript?

Tags:

javascript

I'm binding this to resize function, but how to remove or unbind this from a resize function. I am binding this in some frequently calling method and every time I want to clear this from resize function before bind this. I want bind this to resize function but only time, this can be used in setInterval.

function resize() {
 // some process
}
// I want to unbind this from resize, before going to bind again
resize = resize.bind(this);

like image 906
Bhuvanesh Valarmaan Avatar asked Nov 06 '22 17:11

Bhuvanesh Valarmaan


1 Answers

As bind() creates a new function, you can do the following:

function resize() {
 // some process
}

// Bound
const boundResize = resize.bind(this);

// Unbound
const unboundresize = resize;

Thus the method is not "bound", when you invoke bind() you are creating a new function, not editing the original one context.

The bind() method creates a new function that, when called, has its this keyword set to the provided value, with a given sequence of arguments preceding any provided when the new function is called.

Ref: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind

like image 194
Mosè Raguzzini Avatar answered Nov 15 '22 12:11

Mosè Raguzzini