在jQuery中访问每个功能

edd*_*147 0 jquery

如何在$ .each函数中访问对象变量?

function MyClass()
{
  this.mySetting = 'Hallo';
  this.arr = [{caption: 'Bla', type: 'int'}, {caption: 'Blurr', type: 'float'}];
}

MyClass.prototype.doSomething = function() {

  $.each(this.arr, function() {
    //this here is an element of arr.
    //...but how can I access mySetting??
  });
}
Run Code Online (Sandbox Code Playgroud)

Thi*_*ter 5

存储this在变量中,例如thatself

MyClass.prototype.doSomething = function() {
  var self = this;
  $.each(this.arr, function(idx, elem) {
    // use self here - and do not use this to access the element but elem
  });
}
Run Code Online (Sandbox Code Playgroud)