如何在 JavaScript 中對帶有引數的 typeof 使用?


引數物件是要傳遞給函式的引數。它是所有函式可訪問的一個變數。假設有兩個引數傳遞給一個函式,那麼你可以按以下方式訪問它們

arguments[0]
arguments[1]

同樣,在 JavaScript 中,你可以使用 typeof 帶有引數的方式。首先,讓我們看看如何使用 typeof。typeof 運算子是一個放在其單個運算元之前的單目運算子,運算元的型別可以是任何型別。 

示例

以下程式碼演示瞭如何實現 typeof 運算子

現場演示

<html>
   <body>
      <script>
         var a = 20;
         var b = "String";
         var linebreak = "<br />";

         result = (typeof b == "string" ? "B is String" : "B is Numeric");
         document.write("Result => ");
         document.write(result);
         document.write(linebreak);

         result = (typeof a == "string" ? "A is String" : "A is Numeric");
         document.write("Result => ");
         document.write(result);
         document.write(linebreak);
      </script>
   </body>
</html>

現在讓我們看看如何在 JavaScript 中將 typeof 與引數一起使用。typeof 引數將返回如下物件

document.write(typeof arguments);

假設你有兩個引數,那麼你可以使用 typeof 之類的引用它們,它將返回 typeof 引數。

document.write(typeof arguments[0]);
document.write(typeof arguments[1]);

更新於: 09-Jan-2020

720 次瀏覽

開啟你的職業生涯

完成課程,獲得認證

開始
廣告
© . All rights reserved.