mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2025-04-27 07:33:57 +00:00
[javascript/en] Added for/in loop JavaScript
more explanation about for/in java script.
This commit is contained in:
parent
fe52573118
commit
f1711ddd4c
@ -218,13 +218,26 @@ for (var i = 0; i < 5; i++){
|
|||||||
// will run 5 times
|
// will run 5 times
|
||||||
}
|
}
|
||||||
|
|
||||||
//The For/In statement loops through the properties of an object:
|
//The For/In statement loops iterates over every property across the entire prototype chain
|
||||||
var description = "";
|
var description = "";
|
||||||
var person = {fname:"Paul", lname:"Ken", age:18};
|
var person = {fname:"Paul", lname:"Ken", age:18};
|
||||||
for (var x in person) {
|
for (var x in person) {
|
||||||
description += person[x] + " ";
|
description += person[x] + " ";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//If only want to consider properties attached to the object itself,
|
||||||
|
//and not its prototypes use hasOwnProperty() check
|
||||||
|
var description = "";
|
||||||
|
var person = {fname:"Paul", lname:"Ken", age:18};
|
||||||
|
for (var x in person) {
|
||||||
|
if( person.hasOwnProperty( x ) ) {
|
||||||
|
description += person[x] + " ";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//for/in should not be used to iterate over an Array where the index order is important.
|
||||||
|
//There is no guarantee that for/in will return the indexes in any particular order
|
||||||
|
|
||||||
// && is logical and, || is logical or
|
// && is logical and, || is logical or
|
||||||
if (house.size == "big" && house.colour == "blue"){
|
if (house.size == "big" && house.colour == "blue"){
|
||||||
house.contains = "bear";
|
house.contains = "bear";
|
||||||
|
Loading…
Reference in New Issue
Block a user