From fe525731183779ef5b76cc714c47bd9033953e46 Mon Sep 17 00:00:00 2001 From: Raphael Nascimento Date: Tue, 6 Oct 2015 20:51:47 -0300 Subject: Update javascript.html.markdown For/In loop JavaScript --- javascript.html.markdown | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'javascript.html.markdown') diff --git a/javascript.html.markdown b/javascript.html.markdown index ba2e8ce4..3308b08d 100644 --- a/javascript.html.markdown +++ b/javascript.html.markdown @@ -218,6 +218,13 @@ for (var i = 0; i < 5; i++){ // will run 5 times } +//The For/In statement loops through the properties of an object: +var description = ""; +var person = {fname:"Paul", lname:"Ken", age:18}; +for (var x in person) { + description += person[x] + " "; +} + // && is logical and, || is logical or if (house.size == "big" && house.colour == "blue"){ house.contains = "bear"; -- cgit v1.2.3 From f1711ddd4c98633f059558b8d13e2532afbb5ba7 Mon Sep 17 00:00:00 2001 From: Raphael Nascimento Date: Tue, 6 Oct 2015 23:44:30 -0300 Subject: [javascript/en] Added for/in loop JavaScript more explanation about for/in java script. --- javascript.html.markdown | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'javascript.html.markdown') 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"; -- cgit v1.2.3 From a793d16e3725bdaaf7ca40ff35a7cf0e1aba8488 Mon Sep 17 00:00:00 2001 From: Gabriel Halley Date: Wed, 7 Oct 2015 22:22:12 -0400 Subject: remove extra whitespace, explain NaN --- javascript.html.markdown | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'javascript.html.markdown') diff --git a/javascript.html.markdown b/javascript.html.markdown index ba2e8ce4..754832f1 100644 --- a/javascript.html.markdown +++ b/javascript.html.markdown @@ -64,7 +64,7 @@ doStuff() // There are three special not-a-real-number values: Infinity; // result of e.g. 1/0 -Infinity; // result of e.g. -1/0 -NaN; // result of e.g. 0/0 +NaN; // result of e.g. 0/0, stands for 'Not a Number' // There's also a boolean type. true; @@ -189,7 +189,7 @@ myObj.myFourthKey; // = undefined /////////////////////////////////// // 3. Logic and Control Structures -// The syntax for this section is almost identical to Java's. +// The syntax for this section is almost identical to Java's. // The `if` structure works as you'd expect. var count = 1; @@ -231,8 +231,8 @@ var name = otherName || "default"; // The `switch` statement checks for equality with `===`. -// use 'break' after each case -// or the cases after the correct one will be executed too. +// use 'break' after each case +// or the cases after the correct one will be executed too. grade = 'B'; switch (grade) { case 'A': @@ -516,12 +516,12 @@ more about how to use JavaScript in web pages, start by learning about the [Document Object Model](https://developer.mozilla.org/en-US/docs/Using_the_W3C_DOM_Level_1_Core) -[Learn Javascript by Example and with Challenges](http://www.learneroo.com/modules/64/nodes/350) is a variant of this reference with built-in challenges. +[Learn Javascript by Example and with Challenges](http://www.learneroo.com/modules/64/nodes/350) is a variant of this reference with built-in challenges. [JavaScript Garden](http://bonsaiden.github.io/JavaScript-Garden/) is an in-depth guide of all the counter-intuitive parts of the language. -[JavaScript: The Definitive Guide](http://www.amazon.com/gp/product/0596805527/) is a classic guide / reference book. +[JavaScript: The Definitive Guide](http://www.amazon.com/gp/product/0596805527/) is a classic guide / reference book. In addition to direct contributors to this article, some content is adapted from Louie Dinh's Python tutorial on this site, and the [JS -- cgit v1.2.3 From c3914e277bafb320a37617c4a41984462be1a20d Mon Sep 17 00:00:00 2001 From: Raphael Nascimento Date: Thu, 8 Oct 2015 18:34:03 -0300 Subject: Added for/in loop JavaScript Fixing code style --- javascript.html.markdown | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'javascript.html.markdown') diff --git a/javascript.html.markdown b/javascript.html.markdown index f7a662a4..0e38be8f 100644 --- a/javascript.html.markdown +++ b/javascript.html.markdown @@ -221,7 +221,7 @@ for (var i = 0; i < 5; i++){ //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) { +for (var x in person){ description += person[x] + " "; } @@ -229,8 +229,8 @@ for (var x in person) { //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 ) ) { +for (var x in person){ + if (person.hasOwnProperty(x)){ description += person[x] + " "; } } -- cgit v1.2.3 From 80bb3a0642c88f70c695c93ccb72c33f9667b4fb Mon Sep 17 00:00:00 2001 From: bharathkkb Date: Fri, 16 Oct 2015 11:41:52 -0700 Subject: Added backticks for a keyword. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Added backticks for the “this” keyword. --- javascript.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'javascript.html.markdown') diff --git a/javascript.html.markdown b/javascript.html.markdown index 6ea0b0bb..34ba9b47 100644 --- a/javascript.html.markdown +++ b/javascript.html.markdown @@ -413,7 +413,7 @@ var doubler = product.bind(this, 2); doubler(8); // = 16 // When you call a function with the `new` keyword, a new object is created, and -// made available to the function via the this keyword. Functions designed to be +// made available to the function via the `this` keyword. Functions designed to be // called like that are called constructors. var MyConstructor = function(){ -- cgit v1.2.3