To my surprise, I wrote the code,
function printThis() {
console.log(this);
}
const f = printThis.bind(5).bind(7);
f(); // prints 5 instead of 7
f.call(9); // still prints 5
This creates a problem when a function is already bound in event handlers and can cause nightmares if some function did this and broke the library. Ideally it should either thrown an error or there should be a way to detect if function is already bound.
So how will I know if function is already bound?
Though this is a good interview question, a part of JS I didn't know.
When you use bind it does not run your function just it sets the data 5,
So,
printThis.bind(5)
set 5 to 'this' then it does not return the exact function becuse it returns prototype undefined.So you cannot use again, you can think like a constant variable, it's set one time then you can not set again.
As a result, I write a function rbind means 're bind'.
It simply run the function first then returns the function which is not changed.