Home > Web Front-end > JS Tutorial > Pitfalls of for..in loops in JavaScript

Pitfalls of for..in loops in JavaScript

高洛峰
Release: 2016-11-28 13:33:39
Original
1319 people have browsed it

R The for..in cycle trap in JavaScript

Everyone knows that there are two ways toe objects in JavaScript:

(1) for loop;

(2) for..in cycle;

Using a for loop to iterate array objects must be commonplace for everyone. However, when using for..in loops, everyone should pay attention. Why do you say this? Everyone, listen to my story...

javascript provides a special loop (that is, the for..in loop), which is used to iterate the properties of an object or each element of an array, for... The loop counter in the in loop is a string, not a number. It contains the name of the current property or the index of the current array element.

Case 1:

               //使用for..in循环遍历对象属性
 
               varperson={
 
                       name: "Admin",
 
                       age: 21,
 
                       address:"shandong"
 
               };
 
              
 
               for(vari in person){
 
                       console.log(i);
 
               }
Copy after login

The execution result is:

name

age

address

When traversing an object When , the variable i, which is the loop counter, is the object Attribute name

               //使用for..in循环遍历数组
 
               vararray = ["admin","manager","db"]
 
               for(vari in array){
 
                       console.log(i);
 
               }
Copy after login

Execution result:

0

1

2

When traversing an array, the variable i is the loop count is the current array element Index

Case 2:

But now it seems that the for.. in loop is quite easy to use, but don’t be too happy too early, take a look at the following example:

                 var array =["admin","manager","db"];
 
                 //给Array的原型添加一个name属性
 
                 Array.prototype.name= "zhangsan";
 
                 for(var i in array){
 
                    alert(array[i]);
 
                 }
Copy after login

Running results:

admin

manager

db

zhangsan

Wow, it’s amazing, why did a zhangsan appear out of nowhere

Now, look again What happens when using a for loop?

               vararray = ["admin","manager","db"];
 
                //给Array的原型添加一个name属性
 
               Array.prototype.name = "zhangsan";
 
               for(var i =0 ; i<array.length; i++){
 
                      alert(array[i]);
               };
Copy after login

Run result:

admin

manager

db

Oh, now I understand, the for..in loop will put the method in the prototype of a certain type with the properties being iterated over, so this may cause unexpected errors in your code. In order to avoid this problem, we can use the object's hasOwnProperty() method to avoid this problem. If the object's properties or methods are non-inherited, then the hasOwnProperty() method returns true. That is, the check here does not involve properties and methods inherited from other objects, but only properties created directly in the specific object itself.

Case 3:

               vararray = ["admin","manager","db"];
 
               Array.prototype.name= "zhangshan";
 
               for(vari in array){
 
                      //如果不是该对象自身直接创建的属性(也就是该属//性是原型中的属性),则跳过显示
 
                       if(!array.hasOwnProperty(i)){
 
                               continue;
 
                        }
 
                        alert(array[i]);
 
               }
Copy after login

Running results:

admin

manager

db

Everything again It's as good as ever, hey, I don't know, comrades, after reading this, what do you think? How do you feel? Do you have the feeling of "pushing away the clouds and seeing the sunny sky", haha

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template