检查参数是否传递给Java Script函数 [英] Check if argument is passed to a Java Script function

查看:57
本文介绍了检查参数是否传递给Java Script函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

调用JS函数

alertStatement()

函数定义

function alertStatement(link) {
    if (link) {
        alert('A');     
    }

    if (link!=null) {
        alert('B');     
    }   
}

这两个声明在Windows环境中都正常运行Tomcat,但没有一个在生产中执行它(Linux服务器)。有没有其他方法来比较变量使其工作?

Both of these statements are working fine in Windows Env with Tomcat, but none of them execute it on production (Linux server). Is there any other way to compare variables to make it working?

我使用以下javascript代码工作了。

I got it working using the following javascript code.

function alertStatement(link) {
    if (link!==undefined){
        alert('A');     
    }
}

所以最后undefined对我有用,出于某种原因null比较不起作用

So at last undefined worked for me , for some reason null comparison didn't work

推荐答案

要查看参数是否具有可用值,只需检查参数是否未定义。这有两个目的。它不仅检查是否传递了某些内容,还检查它是否具有可用值:

To see if the argument has a usable value, just check if the argument is undefined. This serves two purposes. It checks not only if something was passed, but also if it has a usable value:

function alertStatement(link) {
    if (link !== undefined) {
        // argument passed and not undefined
    } else {
        // argument not passed or undefined
    }
}

有些人喜欢使用这样的类型:

Some people prefer to use typeof like this:

function alertStatement(link) {
    if (typeof link !== "undefined") {
        // argument passed and not undefined
    } else {
        // argument not passed or undefined
    }
}

null 是一个特定值。 undefined 如果没有通过将会是什么。

null is a specific value. undefined is what it will be if it is not passed.

如果你只是想知道是否有任何东西通过或者不关心它的价值是什么,你可以使用 arguments.length

If you just want to know if anything was passed or not and don't care what its value is, you can use arguments.length.

function alertStatement(link) {
    if (arguments.length) {
        // argument passed
    } else {
        // argument not passed
    }
}

这篇关于检查参数是否传递给Java Script函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆