JS in the scope of this problem: Common error: XXX function or variable is not defined

Scene: the definition of a global function, but when in use, an error XXX function or variable is not defined, but in fact has been clearly defined in js and correct

Cause of the problem: the scope of the problem because most of the calling procedure this.functionname or this.varname in this point of

Solution: In the function call, the function body refers to this outermost layer is added, var self = this;

Call function:

function(){

  this Self = var;  // this defines the function body refers to the outermost

  if(){

    self.functionB ();   // a second layer, can be used normally

    for(){

      self.functionB ();    // third layer may be used normally

    }

  }

},

functionB(){

},

Guess you like

Origin www.cnblogs.com/rzqy/p/10936427.html