首頁 > web前端 > js教程 > 主體

解釋 JavaScript 中「in」運算子的用途

WBOY
發布: 2023-08-24 14:25:09
轉載
1188 人瀏覽過

解释 JavaScript 中“in”运算符的用途

本教學將教授 JavaScript 中的「in」運算子。 JavaScript中有很多可用的運算符,例如用於執行數學運算的算術運算符、賦值運算符、相等運算符等。 「in」運算子也是其中之一,我們可以使用它來從物件中尋找屬性。

在開始之前,讓我問你一個問題。在使用 JavaScript 編碼時,您是否曾經需要檢查物件屬性是否存在?如果有,你是如何處理的?答案很簡單,您可以使用“in”運算符,它會根據物件是否包含該屬性傳回布林值。

使用「in」運算子檢查物件屬性是否存在

“in”運算子的工作方式與其他運算子相同。它需要兩個操作數。物件屬性作為左操作數,物件本身作為右操作數。

文法

您可以依照下列語法使用「in」運算子檢查物件屬性是否存在。

let object = {
   property: value,
}
let ifExist = "property" in object;
登入後複製

在上面的語法中,您可以看到物件如何包含屬性及其值。值可以是數字、字串、布林值等類型。 ifExist 變數根據屬性是否存在於物件中儲存 true 或 false 布林值。

範例 1

在此範例中,我們建立了包含不同屬性和值的物件。此外,該物件還包含方法。之後,我們使用「in」運算子來檢查物件中是否存在屬性。

在範例輸出中,使用者可以觀察到「in」運算子對於 property1 和 property4 傳回 true,但對於 property7 傳回 false,因為它不存在於物件中。

<html>
<body>
   <h3>Using the <i> in operator </i> to check for the existence of the property in the object.</h3>
   <div id = "output"> </div>
   <script>
      let output = document.getElementById("output");
      let object = {
         property1: "value",
         property2: 20,
         property3: false,
         property4: () => {
            console.log("This is a sample function.");
         },
      };
      let ifExist = "property1" in object;
      output.innerHTML +=
         "The property1 exist in the object " + ifExist + "<br/>";
      ifExist = "property4" in object;
      output.innerHTML +=
         "The property4 exist in the object " + ifExist + "<br/>";
      ifExist = "property7" in object;
      output.innerHTML +=
         "The property7 exist in the object " + ifExist + "<br/>";
   </script>
</body>
</html>
登入後複製

在 JavaScript 中,每個物件都有其原型。原型鏈物件實際上包含了物件中的一些方法和屬性。然而,我們還沒有將這些屬性添加到物件中,但 JavaScript 預設添加了它們。例如,陣列和字串原型包含“length”屬性,物件原型包含“toString”屬性。

範例 2

在下面的範例中,我們建立了類別並在其中定義了建構函式來初始化物件屬性。此外,我們也在表格類別中定義了 getSize() 方法。

之後,我們使用建構子建立了表格類別的物件。我們使用“in”運算子來檢查該屬性是否存在於物件原型中。在 JavaScript 中,每個物件的原型中都包含 toString() 方法,這就是它傳回 true 的原因。

<html>
<body>
   <h3>Using the <i> in operator </i> to check for the existence of the property in the object prototype</h3>
   <div id = "output"> </div>
   <script>
      let output = document.getElementById("output");
      // creating the table class
      class table {
         //  constructor function
         constructor(prop1, prop2, prop3) {
            this.prop1 = prop1;
            this.prop2 = prop2;
            this.prop3 = prop3;
         }
         getSize() {
            return 10;
         }
      }
      //  creating the object of the table class
      let tableObjet = new table("blue", "wood", "four drawers");
      // check if a property exists in the object
      let ifExist = "prop1" in tableObjet;
      output.innerHTML +=
         "The prop1 exists in the constructor properties of tableObject: " +
         ifExist + "</br>";
      // some properties also exist in the object prototype chain.
      ifExist = "length" in tableObjet;
      output.innerHTML +=
      "The length property exists in the constructor properties of tableObject: " 
         + ifExist + "</br>";
      ifExist = "toString" in tableObjet;
      output.innerHTML +=
         "The toString Property exists in the constructor properties of tableObject: " 
         + ifExist + "</br>";
   </script>
</body>
</html>
登入後複製

使用 in 運算子檢查數組中是否存在索引

我們只能對物件使用「in」運算子。數組也是物件的一個實例。使用者可以使用instanceOf或typeOf運算子來檢查陣列類型,它會傳回「Object」。因此,數組中的鍵是數組索引,鍵的值是數組值。

這裡,我們可以使用「in」運算子來檢查陣列中是否存在索引。如果存在,我們就可以存取陣列值以避免 arrayOutOfBound 異常。

文法

使用者可以按照以下語法檢查數組中是否存在索引 -

let array = [10, 20, 30];
let ifExist = 2 in array;
登入後複製

在上面的語法中,運算子前面寫的2是數組索引,而不是值。

範例 3

在下面的範例中,我們建立了陣列並使用 typeOf 運算子檢查陣列的類型,該運算子傳回「Object」。

此外,我們也使用了「in」運算子來檢查陣列原型中是否存在陣列索引和長度屬性。

<html>
<body>
   <h2>Using the <i> in operator </i> to check whether the array index exists.</h2>
   <div id = "output"> </div>
   <script>
      let output = document.getElementById("output");
      let array = [10, 20, "Hello", "Hi", true];
      output.innerHTML += "The type of the array is " + typeof array + "<br/>";
      let ifExist = 2 in array;
      output.innerHTML +=
         "The index 2 exist in the array is " + ifExist + "<br/>";
      ifExist = 7 in array;
      output.innerHTML +=
         "The index 7 exist in the array is " + ifExist + "<br/>";
      ifExist = "length" in array;
      output.innerHTML +=
         "The length property exist in the array is " + ifExist + "<br/>";
   </script>
</body>
</html>
登入後複製

本教學教我們如何對物件和陣列使用「in」運算子。在物件中,使用者可以檢查屬性是否存在,在陣列中,使用者可以使用「in」運算子檢查索引是否存在。

以上是解釋 JavaScript 中「in」運算子的用途的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:tutorialspoint.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!