JS を使用して変数が存在するかどうかを確認する方法

php中世界最好的语言
リリース: 2018-05-29 10:00:49
オリジナル
2736 人が閲覧しました

今回は、JS を使用して変数が存在するかどうかを判断する方法と、JS を使用して変数が存在するかどうかを判断する際の 注意事項 について説明します。以下は実際的なケースです。

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
</head>
<body>
//http://www.jb51.net/article/67551.htm
//判断变量i是否存在 typeof(i)=="undefined"
<script>
  /*---------------------------判断函数是否存在-------------------------------*/
  function isExitsFunction(funcName) {
    try {
      if (typeof(eval(funcName)) == "function") {
        return true;
        //  funcName();
      }
    } catch (e) {
      console.log(eval(funcName) + "+++++++++++++++++我异常了!!!!!!!!");
    }
    return false;
  }
  /*--------------------------------判断是否存在指定变量 -----------------------------------------*/
  function isExitsParamsVariable(variableName) {
    try {
      console.log("variableName.length===" + variableName.length);
      if (variableName.length == 0) {
        console.log(variableName + "===value has no params");//"":length为0
        return false;
      } else {
        console.log(variableName + "======value has params");//0:length为undefined
        return true;
      }
    } catch (e) {
      console.log(variableName + "----我异常了!!!!!!!!");//null,undefined,未赋值的a
    }
    return false;//null,undefined,未赋值的a
  }
  /*---------------------------------判断是否undefined--------------------------------*/
  function isExitsVariable(variableName) {
    console.log("typeof variableName====" + typeof(variableName));
    try {
      if (typeof(variableName) == "undefined") {
        console.log(variableName + "===value is undefined");//undefined,未赋值的a
        return false;
      } else {
        console.log(variableName + "=======value is true");//null,0,""
        return true;
      }
    } catch (e) {
      console.log(variableName + "-------我异常了........");
    }
    return false;
  }
  /*-------------------------------------------------测试数据---------------------------------------------*/
  var a;//声明未初始化,没有长度
  console.log("isExitsParamsVariable(a)" + isExitsParamsVariable(a));
  console.log("isExitsVariable(a)" + isExitsVariable(a));
  console.log("--------------------------------------------------")
  var b = undefined;//没有长度
  console.log("isExitsParamsVariable(b)===" + isExitsParamsVariable(b));
  console.log("isExitsVariable(b)===" + isExitsVariable(b));
  console.log("--------------------------------------------------")
  var c = null;//没有长度
  console.log("isExitsParamsVariable(c)===" + isExitsParamsVariable(c));
  console.log("isExitsVariable(c)===" + isExitsVariable(c));
  console.log("--------------------------------------------------")
  var d = 0;//长度undefined
  console.log("isExitsParamsVariable(d)===" + isExitsParamsVariable(d));
  console.log("isExitsVariable(d)===" + isExitsVariable(d));
  console.log("--------------------------------------------------")
  var e = "";//长度为0
  console.log("isExitsParamsVariable(e)====" + isExitsParamsVariable(e));
  console.log("isExitsVariable(e)===" + isExitsVariable(e));
  console.log("--------------------------------------------------")
  /*未定义声明 f 则log会报错:Uncaught ReferenceError: f is not defined ,不会执行两个判断方法*/
  console.log("isExitsParamsVariable(f)====" + isExitsParamsVariable(f));//f:undefined
  console.log("isExitsVariable(f)===" + isExitsVariable(f));
</script>
</body>
</html>
ログイン後にコピー
この記事の事例を読んだ後は、この方法を習得したと思います。さらに興味深い情報については、php 中国語 Web サイトの他の関連記事に注目してください。

推奨読書:

WeChat アプレットでカスタムの複数選択イベントを実装する方法

select を使用せずに Vue でドロップダウン ボックス関数を実装する方法

以上がJS を使用して変数が存在するかどうかを確認する方法の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!