您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

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

2024/7/26 9:48:52发布25次查看
本教程将教授 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”运算符的用途的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product