javascript - existing function is undefined -


i working hammer.js library: http://hammerjs.github.io/ , using jquery plugin. there few ivents 'panright panleft press' on occurrence of able call function. trying call cardswipe(..) function, consol error saying cardswipe undefined. can't figure out causing this.

/* initiate hammerjs on elements */         inithammer : function()         {                /* create hammer object swipable game cards */             game.$gamecard = $('.game-card-mobile');             game.$gamecardtouch = game.$gamecard.hammer();              game.$gamecardtouch.on("panleft panright press", cardswipe(ev, $(this), data));              },          cardswipe : function(ev, element, data) {             console.log(ev.type);             console.log(element);             console.log(data);         } 

edit: adding game.cardswipe resolved that, error of: ev undefined, ev hammer.js event , should recognized (it if swap function function(ev) {..} )

that's because cardswipe undefined in context, it's part of parent object , accessed this.cardswipe

inithammer : function() {        game.$gamecard = $('.game-card-mobile');     game.$gamecardtouch = game.$gamecard.hammer();      game.$gamecardtouch.on("panleft panright press", this.cardswipe(ev, $(this), data));      }, cardswipe : function(ev, element, data) {     console.log(ev.type);     console.log(element);     console.log(data); } 

it looks you're calling right away, not referencing it, convert this

game.$gamecardtouch.on("events", this.cardswipe);   

or if need arguments, use anonymous function

inithammer : function() {         var self = this;      game.$gamecardtouch.on("panleft panright press", function() {         self.cardswipe(ev, $(self), data);     }); }, cardswipe : function(ev, element, data) {  }  

but have no idea cardswipe returns, , how supposed work ?


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -