javascript - Need help understanding function bind when dealing with objects -


function thefunction() {     console.log(this.some) }  //case 1 var object = {     afun:thefunction.bind({some: 'case 1'}) }  //case 2 var object2 = {     afun:thefunction } object2.afun.bind({some: 'case 2'}); object.afun(); object2.afun(); 

the above returns

case 1 undefined  

i expect be

case 1 case 2 

why working differently expect? have ways work around issue. i'm looking why works way.

the issue bind returns copy of function this set value of first argument rather modifying function directly.

to result expected, change

object2.afun.bind({some: 'case 2'}) 

into

object2.afun = object2.afun.bind({some: 'case 2'}) 

Comments