从另一个函数调用传递参数到函数

时间:2014-08-11 09:10:43

标签: javascript leaflet

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

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

在map.on方法中,我需要能够调用一个名为funcToBeCalled +(b的值)的函数,或者通过它我可以将b作为参数map.on('click',funcToBeCalled(b) );

1 个答案:

答案 0 :(得分:0)

你可以用闭包来做:

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

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

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
}