javascript - How to change the context of function using 'bind'? -
this question has answer here:
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 new function new values 'this'. why not working?
once function binded custom object, cannot changed. can original function unchanged:
function greet(){ return 'hi ' + this.name; } greet1 = greet.bind({name: 'tom'}); greet1(); // hi tom greet1 = greet.bind({name: 'harry'}); greet1(); // hi harry
Comments
Post a Comment