2

I'll explain my issue using an example:

I have an object which is called 'ExampleObj' which returns 3 property 'init', 'age', 'weight' and I need to access from age to weight but for some reason i can't do that. Could you explain me why and how can I achieve the correct result?

EDIT: this is the current code, self.tabAnimation() is working on dom ready but... is not working on "click", even if I use (); check the **** in the code, is the line which triggers me error.

 return {

init: function() {
  var self = this;
  tabs.init();
  self.tabAnimation();

  tabToggler.on('click', self.tabAnimation );

}, 

tabAnimation: function() {
  var self = this;

  var activeTabBars = function() {
    console.log('lol');
    tabItem.find(bars).each(function() {
      var me = this;
      ****self.animateBars(me,1000)****
    });
  }

  animateOnVisible.init(tabItem, activeTabBars);

},

animateBars : function(el, duration) {
  var percentage = $(el).data('value') + "%";
  $(el).animate({
    'width': percentage
  }, duration);
}

}

}

Thank you very much

Davide

  • `self.age; // works` No, `self.age; // no-op`. – T.J. Crowder Sep 28 '15 at 14:21
  • `tabToggler.on('click', self.tabAnimation );` is your problem. The function is not bound to `self`. – Bergi Sep 28 '15 at 14:53
  • it's something weird, when I trigger the click it returns me "lol" (the console log) but not the function called after the CL) .. but on DOM ready it triggers correctly the function self.animateBars(me,1000) – Davide Vago Sep 28 '15 at 14:59

1 Answers1

2

You have to use this:

age: function() {
  console.log('some random log');
  var me = this;
  me.weight;
  me.weight(something);
},

You don't have to assign this to another variable, but it doesn't hurt anything if you do.

Note that in your "init" function,

  self.age;

by itself will do nothing. To call the function, you have to write it

  self.age();
Pointy
  • 405,095
  • 59
  • 585
  • 614