0

Hello I have created a custom waypoint function which works well but I would like the ability to be able to add a function to it.

Here is the working custom waypoint function:

function createWaypoint (triggerElementId, animatedElement, className, offsetVal) {
    var waypoint = new Waypoint({
        element: document.getElementById(triggerElementId),
        handler: function(direction) {
            if (direction === 'down') {
                jQuery(animatedElement).addClass(className);
                this.destroy();
            }
        },
        offset: offsetVal
    });
}

//Waypoint Instances
createWaypoint("x", ".y", "z", 500);

Next I would like the add the ability to add a function to the if statement and here is what I came up with:

function createWaypoint (triggerElementId, animatedElement, className, offsetVal, functionName) {
    var waypoint = new Waypoint({
        element: document.getElementById(triggerElementId),
        handler: function(direction) {
            if (direction === 'down') {
                jQuery(animatedElement).addClass(className);
                functionName();
                this.destroy();
            }
        },
        offset: offsetVal
    });
}

function test() {
    alert('Hello World');
}

//Waypoint Instances
createWaypoint("x", ".y", "z", 500);
createWaypoint("x", null, null, null, test);

I added the functionName on line 1 and 7. Then I tried to call it on the very last line. The function "test" does not trigger and I get the error:

Uncaught TypeError: functionName is not a function.

Can anyone help out with this?

Thanks!

cup_of
  • 5,033
  • 5
  • 27
  • 67

1 Answers1

1

Try this with .call or .apply (more about them two):

function createWaypoint (triggerElementId, animatedElement, className, offsetVal, functionName) {
    var waypoint = new Waypoint({
        element: document.getElementById(triggerElementId),
        handler: function(direction) {
            if (direction === 'down') {
                jQuery(animatedElement).addClass(className);

                if(typeof functionName === 'function') {
                    functionName.call();
                } else {
                    console.log('functionName parameter is not a function.');
                }

                this.destroy();
            }
        },
        offset: offsetVal
    });
}

function test() {
    alert('Hello World');
}

//Waypoint Instances
createWaypoint("x", ".y", "z", 500);
createWaypoint("x", null, null, null, test);

EDIT: Actually, you should use functionName.call(undefined) or pass a this parameter from the scope of the function of your desire. It can be from the callback of the handler function, or from the createWaypoint, ie. functionName.call(this) or functionName.call(mappedThis).

As MDN documentation states:

thisArg — The value of this provided for the call to a function. Note that this may not be the actual value seen by the method: if the method is a function in non-strict mode , null and undefined will be replaced with the global object and primitive values will be converted to objects.

Zlatan Omerović
  • 3,184
  • 4
  • 30
  • 58