1
function greet(){
   return 'Hi ' + this.name;
}
greet = greet.bind({name: 'Tom'});
greet(); // Hi Tom
greet = greet.bind({name: 'Harry'});
greet(); // Hi Tom (Why??)

'bind' should return a new function with new values for 'this'. Why is this not working?

Chirag Kothari
  • 1,390
  • 9
  • 19

1 Answers1

1

Once a function is binded to a custom object, it cannot be changed. This is what you can do where the original function is unchanged:

function greet(){
  return 'Hi ' + this.name;
}
greet1 = greet.bind({name: 'Tom'});
greet1(); // Hi Tom
greet1 = greet.bind({name: 'Harry'});
greet1(); // Hi Harry
kingshuk basak
  • 423
  • 2
  • 8