从javascript中的子类回调访问主类属性

时间:2017-07-23 07:54:21

标签: javascript class object

Javascript代码 我想在媒体回调函数中使用this.progressbar

  class CatMusicPlayer {

  constructor(url,playPauseBtn,progressbar,progressTime,durationTime) {
    this.url = url;
    this.playPauseBtn = playPauseBtn;
    this.progressbar = progressbar;
    this.progressTime = progressTime;
    this.durationTime = durationTime;

    new Media(url,null,this.logerror,function(mediaStatus){

        /*
            i want to use this.progressbar,
            but in this block "this" reffers to Media class.
            Media is kind of another class similar to CatMusicPlayer
        */

    });
  }

2 个答案:

答案 0 :(得分:2)

'QuerySet' object has no attribute 'name'的值缓存到另一个变量this  然后使用它const _thisMP = this;

或者在您的情况下,您甚至可以直接致电_thisMP.progressbar();,因为它可以访问

progressbar()

答案 1 :(得分:0)

class CatMusicPlayer {

  constructor(url,playPauseBtn,progressbar,progressTime,durationTime) {
    this.url = url;
    this.playPauseBtn = playPauseBtn;
    this.progressbar = progressbar;
    this.progressTime = progressTime;
    this.durationTime = durationTime;
    let self = this;
    new Media(url,null,this.logerror,function(mediaStatus){
       self.progressbar();


    });
  }