summaryrefslogtreecommitdiffhomepage
path: root/javascript.html.markdown
diff options
context:
space:
mode:
authorRaphael Nascimento <raphaelbn10@gmail.com>2015-10-06 23:44:30 -0300
committerRaphael Nascimento <raphaelbn10@gmail.com>2015-10-06 23:44:30 -0300
commitf1711ddd4c98633f059558b8d13e2532afbb5ba7 (patch)
tree0dd7837919d47f311c33615965a1cbe75581bf28 /javascript.html.markdown
parentfe525731183779ef5b76cc714c47bd9033953e46 (diff)
[javascript/en] Added for/in loop JavaScript
more explanation about for/in java script.
Diffstat (limited to 'javascript.html.markdown')
-rw-r--r--javascript.html.markdown15
1 files changed, 14 insertions, 1 deletions
diff --git a/javascript.html.markdown b/javascript.html.markdown
index 3308b08d..f7a662a4 100644
--- a/javascript.html.markdown
+++ b/javascript.html.markdown
@@ -218,13 +218,26 @@ for (var i = 0; i < 5; i++){
// 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 person = {fname:"Paul", lname:"Ken", age:18};
for (var x in person) {
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
if (house.size == "big" && house.colour == "blue"){
house.contains = "bear";