Skatch
Skatch

Reputation: 2272

Using callback arguments

I have a custom jQuery function that gets an input options object, and a callback function to run after it's completed. Also, inside the jQuery function, I have created a callback object that contains functions which I'd like to be available in the callback function.

keyboardInput : function(input, callback){
    // ...
    if(typeof callback === 'function') {
        callback.call(self.callback);
    }
    // ...
    self.callback = {
        hideCursor : function(){self.hiddenCursor = true;},
        showCursor : function(){self.hiddenCursor = false;},
    }
}

So I call my jQuery function, and the callback runs, but I do not have the self.callback object inside.

$('.txt-fx.keyboard-input.first').keyboardInput({
    speed : [60, 120],
    cursor : 'box',
    cursorBorderWidth : 3,
    delay : 2000,
}, function(obj){
    setTimeout(function(){
        obj.hideCursor();
        $('.txt-fx.keyboard-input.second').keyboardInput({
            speed : [60, 120],
            cursor : 'box',
            cursorBorderWidth : 3,
           delay : 2000,
        });
    }, 3000)
});

throws:

Uncaught TypeError: Cannot read property 'hideCursor' of undefined

What am I doing wrong?

Upvotes: 0

Views: 33

Answers (1)

Jaromanda X
Jaromanda X

Reputation: 1

syntax for .call is fn.call(thisarg, parameter1, parameter1 ...)

you are setting the this for your callback as self.callback - see MDN Documentation of function.call

you probably want to do:

callback.call(null, self.callback);

Upvotes: 1

Related Questions