简体   繁体   English

如何检查传递给javascript函数的参数值是否为空?

[英]How can I check if the value of a parameter passed to a javascript function is null?

I have the following javascript code: 我有以下javascript代码:

    function changeButtonState(targetSelector, action, iconClass) {
        var $target = $(targetSelector);
        var $targetSpan = $(targetSelector + ' span');
        $targetSpan.removeClass('sprite-blank').addClass(iconClass);

How can I make it so that the $targetSpan.removeClass(..).addClass only work if the iconClass has a value when the function is called. 我如何才能使$targetSpan.removeClass(..).addClass仅在调用函数时iconClass具有值时才起作用。 I guess what I am confused about is do I check if it is defined or do I check if it has a length of 0 or more? 我想我感到困惑的是,我是否检查它是否已定义,或者我是否检查其长度是否为0或更长?

It would be best if you check for undefined as well. 如果你检查undefined也是最好的。

function changeButtonState(targetSelector, action, iconClass) {    
    var $target = $(targetSelector);
    if (typeof iconClass !== "undefined" && iconClass) {
        var $targetSpan = $(targetSelector + ' span');
        $targetSpan.removeClass('sprite-blank').addClass(iconClass);
    }
}​

Update Even though this is a very old answer, it is still relevant. 更新即使这是一个非常古老的答案,它仍然是相关的。 Therefore, i would like to update my answer with an improved one based on @jeremy's comment. 因此,我想根据@ jeremy的评论更新我的答案。

function changeButtonState(targetSelector, action, iconClass) {    
    var $target = $(targetSelector);
    //this will not throw exception since iconClass is implicitly declared through the input parameter 
    if (iconClass) {
        var $targetSpan = $(targetSelector + ' span');
        $targetSpan.removeClass('sprite-blank').addClass(iconClass);
    }
}​

This would allow the user (most likely you) to specify whether or not you want it to be active by either giving it a falsey value or a truthy value. 这将允许用户(很可能是您)通过赋予其假名值或真值来指定您是否希望它处于活动状态。 You may also want to check if it's undefined by using the typeof operator. 您可能还想通过使用typeof运算符来检查它是否未定义。

if(iconClass) {
    $targetSpan.removeClass('sprite-blank').addClass(iconClass);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用传递给JavaScript函数的输入参数值到此JS函数打开的弹出窗口中? - How can I use the input parameter value passed to a JavaScript function into the popup opened by this JS function? 如何检查传递给 JavaScript 中 function 的参数类型? - How to check the type of a parameter passed to a function in JavaScript? 如何在JavaScript函数中使用传递的参数? - How can I use a passed parameter in a JavaScript function? Javascript。 如何检查是否在 function 中传递了错误的 boolean 参数? - Javascript. How to check if a false boolean parameter is passed in a function? 如何检查作为Javascript参数传递的回调函数是否有参数? - How to check if a callback function passed as a parameter in Javascript has arguments or not? Javascript:如何检查 DOM 输入值作为 if 语句中的参数 - Javascript: How can I check DOM input value as a parameter in if statement 如何编写一个JavaScript函数,该函数将调用作为参数传递的另一个函数? - How can I write a JavaScript function that will call another function passed as a parameter? 如何检查值是否已传递给javascript函数 - How to check if a value has been passed on to a javascript function or not 如何检查是否定义了传递给JavaScript函数的值或其长度> = 0? - How to check if a value passed to a JavaScript function is defined or its length is >=0? 如何使用javascript访问在函数中传递的对象参数? - How do I access object parameter passed in a function using javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM