开发者

Access this in jquery each function

开发者 https://www.devze.com 2023-02-09 22:44 出处:网络
How can I acces my object variables within a $.each function? function MyClass() { this.mySetting = \'Hallo\';

How can I acces my object variables within a $.each function?

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

MyClass.prototype.doSomething 开发者_JAVA百科= function() {

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


Store this in a variable, e.g. called that or self:

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
  });
}
0

精彩评论

暂无评论...
验证码 换一张
取 消