将参数从另一个函数调用传递给函数

Pass parameter to function frmo another function call

本文关键字:函数 函数调用 参数 另一个      更新时间:2023-09-26

我试图通过另一个函数调用将参数传递给函数。

function cursorViaFun(b){
                map.off('click');
                map.on('click', funcToBeCalled);
            }

在地图上。在方法上,我需要能够调用一个名为functobeccalled +(b的值)的函数,或者通过它我可以将b作为参数映射传递。("点击",funcToBeCalled (b));

您可以使用闭包:

function cursorViaFun(b){
    map.off('click');
    map.on('click', function(){
       funcToBeCalled(b)
    });
}

或者使用bind语法(并非在所有浏览器中都可用):

function cursorViaFun(b){
    map.off('click');
    map.on('click', funcToBeCalled.bind(this, b)); // the first parameter identifies what 
                                                   // this will point inside the function, 
                                                   // here I'm just passing the current 
                                                   // value
}