Overwritten “this” variable problem or how to call a member function?

前端 未结 4 1263
小鲜肉
小鲜肉 2020-12-21 23:16

I have this class where I am using a combination of jQuery and Prototype:

var MyClass = Class.create({
    initElements: function(sumEl) {
       this.sumEl          


        
4条回答
  •  佛祖请我去吃肉
    2020-12-21 23:49

    Totally untested suggestion:

    sumEl.keyup(this.updateSumHandler.bind(this));
    

    .bind() gives back a new function where the first parameter of bind is closured for you as the function's this context. It can also closure parameters, check out the documentation.

    To me, Function.bind() is the single best function ever written in JavaScript :)

提交回复
热议问题