Reputation: 1481
I am new to OOP in jQuery.
I have following class
/*
* myClass
*/
var AlcoholOrder = function (options) {
/*
* Variables accessible
* in the class
*/
var vars = {
myVar: 'original Value'
};
/*
* Can access this.method
* inside other methods using
* root.method()
*/
var root = this;
/*
* Constructor
*/
this.construct = function (options) {
$.extend(vars, options);
};
var addRemoveFavorite = function(){
alert('function called');
};
$(function () {
$(document.body).on('click', '.favorite-add', this.addRemoveFavorite);
});
/*
* Pass options when class instantiated
*/
this.construct(options);
};
Now I am initialising my class in one page with following code.
$(document).ready(function($){
var alcohol = new AlcoholOrder({ myVar : 'new Value' });
});
I want to call addRemoveFavorite
method when click event fired. Currently when I click I am getting error
jquery-1.12.4.min.js:3 Uncaught TypeError: ((n.event.special[g.origType] || {}).handle || g.handler).apply is not a function
I don't know how to call class method on click event. I have searched but not getting proper solution.
Upvotes: 0
Views: 312
Reputation: 2537
This isn't specific to jQuery. The trouble is that you're passing undefined
as the event handler, because you defined addRemoveFavorite
as a local variable, not an owned or inherited property. So this.addRemoveFavorite
is not found, and undefined
is substituted.
Upvotes: 1