javascript - 如何创建带有未定义参数的 JavaScript 函数?

标签 javascript parameters

我发现了有关创建带有可选参数的函数的类似问题。但我不断遇到错误是参数可能不存在。我有一个交互式 map ,它可以做很多事情。但是,如果 outputlayer 为 null,则该函数将不会执行。该层可能存在也可能不存在。我似乎无法弄清楚如何传递这个可选参数(如果它不存在)。感谢您的帮助!

function appdelete(selected,vectorlayer,drawFeature,outputlayer) {

    $(selected).prev().children().children().eq(2).val('Latitude');
    $(selected).prev().children().children().eq(4).val('Longitude');
    vectorlayer.destroyFeatures();
    drawFeature.activate();

    try {
        map.removeLayer(outputlayer);
    } catch(err) { };
}

最佳答案

首先检查它是否存在,也许还有类型。

if(outputlayer && "object" === typeof outputlayer) {
    map.removeLayer(outputlayer);
}

关于javascript - 如何创建带有未定义参数的 JavaScript 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13705476/

相关文章:

javascript - 为什么在递归异步函数中添加 then 会使其返回未定义?

javascript - 新的 Apple 页面如何在图像中淡出?

ruby-on-rails - 在 ruby​​ 中使用 params.require 保存多条记录

python - 构造函数参数与方法调用

传递到新窗口的 JavaFX 参数变为 NULL

.net - CustomAttributeFormatException : Binary format of the specified custom attribute was invalid

c++ - 带有额外可选模板参数的标准库容器?

javascript - 如何使用 jQuery 拆分表单数据并分配给另一个值?

javascript - 使用 Bootstrap 按钮验证错误

javascript - 在 JQuery 中将代码应用于其父 div 的内部元素单击问题