javascript context issue - parameters undefined

2020-04-16 02:03发布

I have this code:

targetMu: function(programmeCode, muId) {

//Parameter values are fine here

  targetMuController.targetMuView.on("targetMu:afterRender", function(programmeCode, muId) {       
        this.renderCustomWidgets(muId, programmeCode);
  });
}

When this.renderCustomWidgets(muId, programmeCode) gets fired, programmeCode & muId is undefined, why?

And how can I fix this?

2条回答
爷、活的狠高调
2楼-- · 2020-04-16 02:44

try to remove parameters from anonymous function:

targetMu: function(programmeCode, muId) {
    targetMuController.targetMuView.on("targetMu:afterRender", function() {       
        this.renderCustomWidgets(muId, programmeCode);
    });
}
查看更多
走好不送
3楼-- · 2020-04-16 02:46
, function(programmeCode, muId) {

You just declared new parameters in the callback function with the same names.

Inside the callback, these names refer to the inner parameters – whatever was passed to the callback.

查看更多
登录 后发表回答