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 841dcb5f0985f2b25a2d0b2285b03777cefd1a4f Mon Sep 17 00:00:00 2001 From: Xander Smalbil Date: Thu, 15 Oct 2015 22:38:44 +0200 Subject: Updated javascript.html.markdown Added some additional information on declaring variables. --- javascript.html.markdown | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'javascript.html.markdown') diff --git a/javascript.html.markdown b/javascript.html.markdown index 6ea0b0bb..a8ca415e 100644 --- a/javascript.html.markdown +++ b/javascript.html.markdown @@ -144,6 +144,10 @@ someOtherVar = 10; // Variables declared without being assigned to are set to undefined. var someThirdVar; // = undefined +// if you wan't to declare a couple of variables, then you could use a comma +// separator +var someFourthVar = 2, someFifthVar = 4; + // There's shorthand for performing math operations on variables: someVar += 5; // equivalent to someVar = someVar + 5; someVar is 10 now someVar *= 10; // now someVar is 100 -- 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 From 53366ebdbeecb502131c2768979e4b6ed9d59d9f Mon Sep 17 00:00:00 2001 From: venegu Date: Sat, 17 Oct 2015 22:21:25 -0400 Subject: Adding modulo division to JavaScript article --- javascript.html.markdown | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) (limited to 'javascript.html.markdown') diff --git a/javascript.html.markdown b/javascript.html.markdown index 34ba9b47..937354eb 100644 --- a/javascript.html.markdown +++ b/javascript.html.markdown @@ -54,6 +54,11 @@ doStuff() // Including uneven division. 5 / 2; // = 2.5 +// And modulo division. +10 % 2; // = 0 +30 % 4; // = 2 +18.5 % 7; // = 4.5 + // Bitwise operations also work; when you perform a bitwise operation your float // is converted to a signed int *up to* 32 bits. 1 << 2; // = 4 @@ -104,7 +109,7 @@ null == undefined; // = true // ...unless you use === "5" === 5; // = false -null === undefined; // = false +null === undefined; // = false // ...which can result in some weird behaviour... 13 + !0; // 14 @@ -220,15 +225,15 @@ 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}; +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, +//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}; +var person = {fname:"Paul", lname:"Ken", age:18}; for (var x in person){ if (person.hasOwnProperty(x)){ description += person[x] + " "; -- cgit v1.2.3 From c23bba2b6010e659d518f144d689102d0e9fb147 Mon Sep 17 00:00:00 2001 From: Cameron Wood Date: Sun, 18 Oct 2015 05:46:35 -0400 Subject: [javascript/en] Small typo fix Just a simple 1-word typo fix --- 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 937354eb..9c4f06fc 100644 --- a/javascript.html.markdown +++ b/javascript.html.markdown @@ -324,7 +324,7 @@ i; // = 5 - not undefined as you'd expect in a block-scoped language // scope. (function(){ var temporary = 5; - // We can access the global scope by assiging to the "global object", which + // We can access the global scope by assigning to the "global object", which // in a web browser is always `window`. The global object may have a // different name in non-browser environments such as Node.js. window.permanent = 10; -- cgit v1.2.3 From 3835cd26f5c58b05c547d2c9d51b39252e0eca15 Mon Sep 17 00:00:00 2001 From: Sawyer Charles Date: Mon, 19 Oct 2015 10:54:42 -0500 Subject: [javascript/en] Added setInterval Added the setInterval function provided by most browsers --- javascript.html.markdown | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'javascript.html.markdown') diff --git a/javascript.html.markdown b/javascript.html.markdown index d408e885..22a2959c 100644 --- a/javascript.html.markdown +++ b/javascript.html.markdown @@ -310,6 +310,12 @@ setTimeout(myFunction, 5000); // Note: setTimeout isn't part of the JS language, but is provided by browsers // and Node.js. +// Another function provided by browsers is setInterval +function myFunction(){ + // this code will be called every 5 seconds +} +setInterval(myFunction(), 5000); + // Function objects don't even have to be declared with a name - you can write // an anonymous function definition directly into the arguments of another. setTimeout(function(){ -- cgit v1.2.3 From d78518288bd8d37714ff00355274a7527e0e5a66 Mon Sep 17 00:00:00 2001 From: Sawyer Charles Date: Mon, 19 Oct 2015 12:42:23 -0500 Subject: removed () --- 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 22a2959c..81dc09a9 100644 --- a/javascript.html.markdown +++ b/javascript.html.markdown @@ -314,7 +314,7 @@ setTimeout(myFunction, 5000); function myFunction(){ // this code will be called every 5 seconds } -setInterval(myFunction(), 5000); +setInterval(myFunction, 5000); // Function objects don't even have to be declared with a name - you can write // an anonymous function definition directly into the arguments of another. -- cgit v1.2.3 From 7fdce9215acde388da6de41f524aefa8bfb05532 Mon Sep 17 00:00:00 2001 From: Sawyer Charles Date: Mon, 19 Oct 2015 16:00:22 -0500 Subject: Fixed bracket placement --- javascript.html.markdown | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) (limited to 'javascript.html.markdown') diff --git a/javascript.html.markdown b/javascript.html.markdown index 81dc09a9..a119be88 100644 --- a/javascript.html.markdown +++ b/javascript.html.markdown @@ -291,12 +291,9 @@ myFunction("foo"); // = "FOO" // Note that the value to be returned must start on the same line as the // `return` keyword, otherwise you'll always return `undefined` due to // automatic semicolon insertion. Watch out for this when using Allman style. -function myFunction() -{ +function myFunction(){ return // <- semicolon automatically inserted here - { - thisIsAn: 'object literal' - } + {thisIsAn: 'object literal'} } myFunction(); // = undefined -- cgit v1.2.3