>  기사  >  웹 프론트엔드  >  JavaScript에서 "in" 연산자의 목적을 설명하세요.

JavaScript에서 "in" 연산자의 목적을 설명하세요.

WBOY
WBOY앞으로
2023-08-24 14:25:091167검색

解释 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으로 문의하시기 바랍니다. 삭제