How to change the context of a function in javascript

前端 未结 6 1737
醉梦人生
醉梦人生 2020-12-13 19:24

I\'m trying to understand why in javascript, you might want to change the context of a function. I\'m looking for a real world example or something which will help me unders

6条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-13 20:12

    bind function might be what you're looking for, bind function returns a new function with in the context that you passed in , a real world scenario could be when you are using jquery delegates to attach some behavior to a dom element, and you want the callback being execute in a different context. 'cause the default context in a jquery delgate is the dom object that is bound to the handler , which means you can't access any property besides the properties that belongs to the dom object

提交回复
热议问题