The content
- Ask readers
- For in javascript
- The first mistake is to use for in javascript
- Mistake 2
- The third mistake is with iterating enumerable properties.
For in javascript. Loop is an inevitable tip when programming functions. But have you ever made the mistakes that we are about to say below, and how to fix those problems?
Requirements
- Understand basic javascript
- Ever used loop, probably
for
For in javascript
Actually, for iterating in programming, we also said a lot about the positive things of using for in javascript. And we have also compared for in and for of in javascript. If you feel your knowledge is quite gaping, you can read the previous articles again, and then learn about mistakes when using for in this article.
JavaScript for … in to iterate enumerable properties ( enumerable properties mean? ) Of an object as follows:
### with object
var player = {
two: "Messi",
one: "Ronaldo",
zero: "Name"
};
var countdown = "";
for (var step in player) {
countdown += player[step] + "\n";
}
console.log(countdown);
// => "Messi
// Ronaldo
// Name!
// "
Because for in
it works on any javascript object, so we can use it to loop one Array
like the above example, we can do it again like this:
### with array
var player = [
"Messi",
"Ronaldo",
"Name"
]
var countdown = "";
for (var step in player) {
countdown += player[step] + "\n";
}
console.log(countdown);
// => "Messi
// Ronaldo
// Name!
// "
Notes: Two different examples, take a closer look, object and array
The first mistake is to use for in javascript
Array.prototype.voice = "Rooney"; //enumerable properties
var player = [
"Messi",
"Ronaldo",
"Name"
] // there are 3 item
s here
var countdown = "";
for (var step in player) {
countdown += player[step] + "\n";
}
console.log(countdown);
// got 4 items.
//Messi
//Ronaldo
//Name
//Rooney
Many of you are still observant and know that you can solve this simply by using the hasOwnProperty.
Array.prototype.voice = "Rooney"; //enumerable properties
var player = [
"Messi",
"Ronaldo",
"AnonyStick"
] // there are 3 item
s here
var countdown = "";
for (var step in player) {
if (player.hasOwnProperty(step)) {
countdown += player[step] + "\n";
}
}
console.log(countdown);
//got 3 items.
//Messi
//Ronaldo
//Name
But after you settle, can you go smoother and smoother? This question left for you?
Mistake 2
Wrong order when the loop is completed. That is not really a problem for a normal object whose value is inherently out of order. But you probably don’t want your JavaScript engine to return array values in random order because you might get an unexpected result like this:
If you have time to take a look at ecma-international.org I think you will understand. That is not really a problem for an ordinary object whose value is inherently out of order.
But you probably don’t want your JavaScript engine to return your items in random order, because you might get unexpected results like this:
console.log(countdown);
// => "Name
// Ronaldo
// Messi
// "
The third mistake is with iterating enumerable properties.
As we discussed before, it is possible to store additional properties on an array. This can also lead to unexpected results.
For example:
var player = [
"Messi",
"Ronaldo",
"Name"
];
player.announcer = "Rooney";
var countdown = "";
for (var step in player) {
if (player.hasOwnProperty(step)) {
countdown += player[step] + "\n";
}
}
console.log(countdown);
// => "Messi
//Ronaldo
//Name
//Rooney
You can refer to adripofjavascript