0

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.

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Ohh yes. that was my mistake. thanks @llama to guide me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.