Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 添加将功能添加到现有自定义航路点功能中的功能_Javascript_Jquery_Jquery Waypoints - Fatal编程技术网

Javascript 添加将功能添加到现有自定义航路点功能中的功能

Javascript 添加将功能添加到现有自定义航路点功能中的功能,javascript,jquery,jquery-waypoints,Javascript,Jquery,Jquery Waypoints,您好,我已经创建了一个自定义的航路点功能,该功能运行良好,但我希望能够添加一个功能到它 以下是工作自定义航路点功能: function createWaypoint (triggerElementId, animatedElement, className, offsetVal) { var waypoint = new Waypoint({ element: document.getElementById(triggerElementId), handl

您好,我已经创建了一个自定义的航路点功能,该功能运行良好,但我希望能够添加一个功能到它

以下是工作自定义航路点功能:

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);
接下来,我想添加将函数添加到if语句的功能,下面是我想到的:

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);
我在第1行和第7行添加了functionName。然后我试着在最后一行打电话给它。功能测试未触发,我得到错误:

未捕获类型错误:functionName不是函数

有人能帮忙吗

谢谢

尝试使用.call或.apply:

编辑:实际上,您应该使用functionName.callundefined或从所需函数的范围内传递一个this参数。它可以来自处理程序函数的回调,也可以来自createWaypoint,即functionName.callthis或functionName.callmappedThis

如各国所述:

蓟马- 为函数调用提供的该值。请注意,这可能不是该方法看到的实际值:如果该方法是非严格模式下的函数,则null和undefined将替换为全局对象,并且基本值将转换为对象

所以基本上只需更改functionName.call;要调用functionName.callthis@是的。或传递undefined或null,这将使用app/root的作用域。
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);