From e69ca623a73756c39c2fba7d61221fd59a291fa0 Mon Sep 17 00:00:00 2001 From: John Gabriele Date: Tue, 3 Feb 2015 17:31:44 -0500 Subject: javascript/en: added some md formatting Added some markdown formatting to the comments throughout to clarify meaning. Also tweaked a couple of other quote marks as well. Also shortened a couple of long lines. More to do, but stopped here for now. :) --- javascript.html.markdown | 60 +++++++++++++++++++++++++----------------------- 1 file changed, 31 insertions(+), 29 deletions(-) (limited to 'javascript.html.markdown') diff --git a/javascript.html.markdown b/javascript.html.markdown index aabd5e43..f61e6c74 100644 --- a/javascript.html.markdown +++ b/javascript.html.markdown @@ -110,19 +110,19 @@ null === undefined; // = false 13 + !0; // 14 "13" + !0; // '13true' -// You can access characters in a string with charAt +// You can access characters in a string with `charAt` "This is a string".charAt(0); // = 'T' -// ...or use substring to get larger pieces +// ...or use `substring` to get larger pieces. "Hello world".substring(0, 5); // = "Hello" -// length is a property, so don't use () +// `length` is a property, so don't use (). "Hello".length; // = 5 -// There's also null and undefined -null; // used to indicate a deliberate non-value +// There's also `null` and `undefined`. +null; // used to indicate a deliberate non-value undefined; // used to indicate a value is not currently present (although - // undefined is actually a value itself) + // `undefined` is actually a value itself) // false, null, undefined, NaN, 0 and "" are falsy; everything else is truthy. // Note that 0 is falsy and "0" is truthy, even though 0 == "0". @@ -130,8 +130,9 @@ undefined; // used to indicate a value is not currently present (although /////////////////////////////////// // 2. Variables, Arrays and Objects -// Variables are declared with the var keyword. JavaScript is dynamically typed, -// so you don't need to specify type. Assignment uses a single = character. +// Variables are declared with the `var` keyword. JavaScript is dynamically +// typed, so you don't need to specify type. Assignment uses a single `=` +// character. var someVar = 5; // if you leave the var keyword off, you won't get an error... @@ -165,7 +166,7 @@ myArray.length; // = 4 // Add/Modify at specific index myArray[3] = "Hello"; -// JavaScript's objects are equivalent to 'dictionaries' or 'maps' in other +// JavaScript's objects are equivalent to "dictionaries" or "maps" in other // languages: an unordered collection of key-value pairs. var myObj = {key1: "Hello", key2: "World"}; @@ -190,7 +191,7 @@ myObj.myFourthKey; // = undefined // The syntax for this section is almost identical to Java's. -// The if structure works as you'd expect. +// The `if` structure works as you'd expect. var count = 1; if (count == 3){ // evaluated if count is 3 @@ -200,7 +201,7 @@ if (count == 3){ // evaluated if it's not either 3 or 4 } -// As does while. +// As does `while`. while (true){ // An infinite loop! } @@ -211,7 +212,7 @@ do { input = getInput(); } while (!isValid(input)) -// the for loop is the same as C and Java: +// The `for` loop is the same as C and Java: // initialisation; continue condition; iteration. for (var i = 0; i < 5; i++){ // will run 5 times @@ -229,7 +230,7 @@ if (colour == "red" || colour == "blue"){ var name = otherName || "default"; -// switch statement checks for equality with === +// The `switch` statement checks for equality with `===`. // use 'break' after each case // or the cases after the correct one will be executed too. grade = 'B'; @@ -252,14 +253,14 @@ switch (grade) { /////////////////////////////////// // 4. Functions, Scope and Closures -// JavaScript functions are declared with the function keyword. +// JavaScript functions are declared with the `function` keyword. function myFunction(thing){ return thing.toUpperCase(); } 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 +// `return` keyword, otherwise you'll always return `undefined` due to // automatic semicolon insertion. Watch out for this when using Allman style. function myFunction() { @@ -298,8 +299,8 @@ 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 - // in a web browser is always 'window'. The global object may have a + // We can access the global scope by assiging 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; })(); @@ -312,7 +313,7 @@ permanent; // = 10 function sayHelloInFiveSeconds(name){ var prompt = "Hello, " + name + "!"; // Inner functions are put in the local scope by default, as if they were - // declared with 'var'. + // declared with `var`. function inner(){ alert(prompt); } @@ -320,7 +321,7 @@ function sayHelloInFiveSeconds(name){ // setTimeout is asynchronous, so the sayHelloInFiveSeconds function will // exit immediately, and setTimeout will call inner afterwards. However, // because inner is "closed over" sayHelloInFiveSeconds, inner still has - // access to the 'prompt' variable when it is finally called. + // access to the `prompt` variable when it is finally called. } sayHelloInFiveSeconds("Adam"); // will open a popup with "Hello, Adam!" in 5s @@ -336,7 +337,7 @@ var myObj = { myObj.myFunc(); // = "Hello world!" // When functions attached to an object are called, they can access the object -// they're attached to using the this keyword. +// they're attached to using the `this` keyword. myObj = { myString: "Hello world!", myFunc: function(){ @@ -352,7 +353,7 @@ var myFunc = myObj.myFunc; myFunc(); // = undefined // Inversely, a function can be assigned to the object and gain access to it -// through this, even if it wasn't attached when it was defined. +// through `this`, even if it wasn't attached when it was defined. var myOtherFunc = function(){ return this.myString.toUpperCase(); } @@ -360,37 +361,38 @@ myObj.myOtherFunc = myOtherFunc; myObj.myOtherFunc(); // = "HELLO WORLD!" // We can also specify a context for a function to execute in when we invoke it -// using 'call' or 'apply'. +// using `call` or `apply`. var anotherFunc = function(s){ return this.myString + s; } anotherFunc.call(myObj, " And Hello Moon!"); // = "Hello World! And Hello Moon!" -// The 'apply' function is nearly identical, but takes an array for an argument list. +// The `apply` function is nearly identical, but takes an array for an argument +// list. anotherFunc.apply(myObj, [" And Hello Sun!"]); // = "Hello World! And Hello Sun!" -// This is useful when working with a function that accepts a sequence of arguments -// and you want to pass an array. +// This is useful when working with a function that accepts a sequence of +// arguments and you want to pass an array. Math.min(42, 6, 27); // = 6 Math.min([42, 6, 27]); // = NaN (uh-oh!) Math.min.apply(Math, [42, 6, 27]); // = 6 -// But, 'call' and 'apply' are only temporary. When we want it to stick, we can use -// bind. +// But, `call` and `apply` are only temporary. When we want it to stick, we can +// use `bind`. var boundFunc = anotherFunc.bind(myObj); boundFunc(" And Hello Saturn!"); // = "Hello World! And Hello Saturn!" -// Bind can also be used to partially apply (curry) a function. +// `bind` can also be used to partially apply (curry) a function. var product = function(a, b){ return a * b; } var doubler = product.bind(this, 2); doubler(8); // = 16 -// When you call a function with the new keyword, a new object is created, and +// 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 // called like that are called constructors. -- cgit v1.2.3 From 08b0596e45feff0b292f295f52d82a80ab8624c2 Mon Sep 17 00:00:00 2001 From: Nimit Kalra Date: Wed, 25 Feb 2015 10:03:19 -0600 Subject: Add semicolon to maximine consistency. Added a semicolon to conform with statement at top of guide: // Because those cases can cause unexpected results, we'll keep on using // semicolons in this guide. --- 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 f61e6c74..95808d05 100644 --- a/javascript.html.markdown +++ b/javascript.html.markdown @@ -207,7 +207,7 @@ while (true){ } // Do-while loops are like while loops, except they always run at least once. -var input +var input; do { input = getInput(); } while (!isValid(input)) -- cgit v1.2.3 From 5e0964e2eee068b872fb0d61b5398997e0b76670 Mon Sep 17 00:00:00 2001 From: jasonqu Date: Fri, 6 Mar 2015 17:42:44 +0800 Subject: [javascript/zh] Translation tracking --- javascript.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'javascript.html.markdown') diff --git a/javascript.html.markdown b/javascript.html.markdown index 95808d05..588ea86d 100644 --- a/javascript.html.markdown +++ b/javascript.html.markdown @@ -407,7 +407,7 @@ myNewObj.myNumber; // = 5 // look at its prototype. // Some JS implementations let you access an object's prototype on the magic -// property __proto__. While this is useful for explaining prototypes it's not +// property `__proto__`. While this is useful for explaining prototypes it's not // part of the standard; we'll get to standard ways of using prototypes later. var myObj = { myString: "Hello world!" @@ -438,7 +438,7 @@ myObj.myBoolean; // = true myPrototype.meaningOfLife = 43; myObj.meaningOfLife; // = 43 -// We mentioned that __proto__ was non-standard, and there's no standard way to +// We mentioned that `__proto__` was non-standard, and there's no standard way to // change the prototype of an existing object. However, there are two ways to // create a new object with a given prototype. -- cgit v1.2.3 From 87d0c402fb596c64a443a5374335508cb6941dc4 Mon Sep 17 00:00:00 2001 From: Philip Rowan Date: Thu, 1 Oct 2015 18:34:11 -0500 Subject: [javascript] Fix for issue 1248 https://github.com/adambard/learnxinyminutes-docs/issues/1248 --- javascript.html.markdown | 3 --- 1 file changed, 3 deletions(-) (limited to 'javascript.html.markdown') diff --git a/javascript.html.markdown b/javascript.html.markdown index 588ea86d..ba2e8ce4 100644 --- a/javascript.html.markdown +++ b/javascript.html.markdown @@ -475,9 +475,6 @@ myNumber === myNumberObj; // = false if (0){ // This code won't execute, because 0 is falsy. } -if (Number(0)){ - // This code *will* execute, because Number(0) is truthy. -} // However, the wrapper objects and the regular builtins share a prototype, so // you can actually add functionality to a string, for instance. -- cgit v1.2.3 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 From 257533fe5aa5027dc71e78d10833a3ba0febb426 Mon Sep 17 00:00:00 2001 From: ven Date: Wed, 21 Oct 2015 23:18:38 +0200 Subject: fix #1726, thanks @fisktech --- 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 a119be88..c1c59de1 100644 --- a/javascript.html.markdown +++ b/javascript.html.markdown @@ -21,7 +21,7 @@ Feedback would be highly appreciated! You can reach me at [adam@brenecki.id.au](mailto:adam@brenecki.id.au). ```js -// Comments are like C. Single-line comments start with two slashes, +// Comments are like C's. Single-line comments start with two slashes, /* and multiline comments start with slash-star and end with star-slash */ -- cgit v1.2.3 From db4160710dee1ee76d750c35452294c3134061fa Mon Sep 17 00:00:00 2001 From: Hunter Stevens Date: Fri, 23 Oct 2015 13:14:38 -0400 Subject: Remove "feedback", fix link markdown --- javascript.html.markdown | 48 ++++++++++++++++++++++++------------------------ 1 file changed, 24 insertions(+), 24 deletions(-) (limited to 'javascript.html.markdown') diff --git a/javascript.html.markdown b/javascript.html.markdown index c1c59de1..cce488e1 100644 --- a/javascript.html.markdown +++ b/javascript.html.markdown @@ -16,10 +16,6 @@ JavaScript isn't just limited to web browsers, though: Node.js, a project that provides a standalone runtime for Google Chrome's V8 JavaScript engine, is becoming more and more popular. -Feedback would be highly appreciated! You can reach me at -[@adambrenecki](https://twitter.com/adambrenecki), or -[adam@brenecki.id.au](mailto:adam@brenecki.id.au). - ```js // Comments are like C's. Single-line comments start with two slashes, /* and multiline comments start with slash-star @@ -534,28 +530,32 @@ if (Object.create === undefined){ // don't overwrite it if it exists ## Further Reading -The [Mozilla Developer -Network](https://developer.mozilla.org/en-US/docs/Web/JavaScript) provides -excellent documentation for JavaScript as it's used in browsers. Plus, it's a -wiki, so as you learn more you can help others out by sharing your own -knowledge. +The [Mozilla Developer Network][1] provides excellent documentation for +JavaScript as it's used in browsers. Plus, it's a wiki, so as you learn more you +can help others out by sharing your own knowledge. + +MDN's [A re-introduction to JavaScript][2] covers much of the concepts covered +here in more detail. This guide has quite deliberately only covered the +JavaScript language itself; if you want to learn more about how to use +JavaScript in web pages, start by learning about the [Document Object Model][3]. + +[Learn Javascript by Example and with Challenges][4] is a variant of this +reference with built-in challenges. -MDN's [A re-introduction to -JavaScript](https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript) -covers much of the concepts covered here in more detail. This guide has quite -deliberately only covered the JavaScript language itself; if you want to learn -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) +[JavaScript Garden][5] is an in-depth guide of all the counter-intuitive parts +of the language. -[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: The Definitive Guide][6] is a classic guide and reference book. -[JavaScript Garden](http://bonsaiden.github.io/JavaScript-Garden/) is an in-depth -guide of all the counter-intuitive parts of the language. +In addition to direct contributors to this article, some content is adapted from +Louie Dinh's Python tutorial on this site, and the [JS Tutorial][7] on the +Mozilla Developer Network. -[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 -Tutorial](https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript) -on the Mozilla Developer Network. +[1]: https://developer.mozilla.org/en-US/docs/Web/JavaScript +[2]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript +[3]: https://developer.mozilla.org/en-US/docs/Using_the_W3C_DOM_Level_1_Core +[4]: http://www.learneroo.com/modules/64/nodes/350 +[5]: http://bonsaiden.github.io/JavaScript-Garden/ +[6]: http://www.amazon.com/gp/product/0596805527/ +[7]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript -- cgit v1.2.3 From 9c3c3dff4518671e82cd3324c0d6fdddd506dfd3 Mon Sep 17 00:00:00 2001 From: Leonardy Kristianto Date: Wed, 28 Oct 2015 02:04:34 +0800 Subject: Added resources to read for 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 cce488e1..dc573b0e 100644 --- a/javascript.html.markdown +++ b/javascript.html.markdown @@ -547,6 +547,11 @@ of the language. [JavaScript: The Definitive Guide][6] is a classic guide and reference book. +[Eloquent Javascript][8] by Marijn Haverbeke is an excellent JS book/ebook with attached terminal + +[Javascript: The Right Way][9] is a guide intended to introduce new developers to JavaScript and help experienced developers learn more about its best practices. + + In addition to direct contributors to this article, some content is adapted from Louie Dinh's Python tutorial on this site, and the [JS Tutorial][7] on the Mozilla Developer Network. @@ -559,3 +564,5 @@ Mozilla Developer Network. [5]: http://bonsaiden.github.io/JavaScript-Garden/ [6]: http://www.amazon.com/gp/product/0596805527/ [7]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript +[8]: http://eloquentjavascript.net/ +[9]: http://jstherightway.org/ -- cgit v1.2.3 From 61aa6a3e0146faed45230111b0bdcf1b0c2209f8 Mon Sep 17 00:00:00 2001 From: Corban Mailloux Date: Tue, 27 Oct 2015 22:18:02 -0400 Subject: "wan't" -> "want" --- 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 dc573b0e..b54434e0 100644 --- a/javascript.html.markdown +++ b/javascript.html.markdown @@ -145,7 +145,7 @@ 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 +// if you want to declare a couple of variables, then you could use a comma // separator var someFourthVar = 2, someFifthVar = 4; -- cgit v1.2.3 From a8d5105fab5e40923d834fb5848aabf561bc1701 Mon Sep 17 00:00:00 2001 From: Corban Mailloux Date: Tue, 27 Oct 2015 22:47:03 -0400 Subject: [javascript/en] Spacing and capitalization of comments ... and a few grammar fixes. --- javascript.html.markdown | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) (limited to 'javascript.html.markdown') diff --git a/javascript.html.markdown b/javascript.html.markdown index b54434e0..5bac3aa7 100644 --- a/javascript.html.markdown +++ b/javascript.html.markdown @@ -136,7 +136,7 @@ undefined; // used to indicate a value is not currently present (although // character. var someVar = 5; -// if you leave the var keyword off, you won't get an error... +// If you leave the var keyword off, you won't get an error... someOtherVar = 10; // ...but your variable will be created in the global scope, not in the scope @@ -145,7 +145,7 @@ someOtherVar = 10; // Variables declared without being assigned to are set to undefined. var someThirdVar; // = undefined -// if you want to declare a couple of variables, then you could use a comma +// If you want to declare a couple of variables, then you could use a comma // separator var someFourthVar = 2, someFifthVar = 4; @@ -223,15 +223,15 @@ for (var i = 0; i < 5; i++){ // will run 5 times } -//The For/In statement loops iterates over every property across the entire prototype chain +// The for/in statement 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 +// To only consider properties attached to the object itself +// and not its prototypes, use the `hasOwnProperty()` check. var description = ""; var person = {fname:"Paul", lname:"Ken", age:18}; for (var x in person){ @@ -240,8 +240,9 @@ for (var x in person){ } } -//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 +// For/in should not be used to iterate over an Array where the index order +// is important, as 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"){ @@ -256,7 +257,7 @@ var name = otherName || "default"; // The `switch` statement checks for equality with `===`. -// use 'break' after each case +// Use 'break' after each case // or the cases after the correct one will be executed too. grade = 'B'; switch (grade) { -- cgit v1.2.3 From c83eb6c6bca63b28a11b975cc64db5723e94b240 Mon Sep 17 00:00:00 2001 From: Adam Brenecki Date: Wed, 28 Oct 2015 13:26:24 +1030 Subject: [javascript/en] Move comparisons to other languages into preamble --- javascript.html.markdown | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) (limited to 'javascript.html.markdown') diff --git a/javascript.html.markdown b/javascript.html.markdown index 5bac3aa7..3f9eb641 100644 --- a/javascript.html.markdown +++ b/javascript.html.markdown @@ -16,9 +16,14 @@ JavaScript isn't just limited to web browsers, though: Node.js, a project that provides a standalone runtime for Google Chrome's V8 JavaScript engine, is becoming more and more popular. +JavaScript has a C-like syntax, so if you've used languages like C or Java, +a lot of the basic syntax will already be familiar. Despite this, and despite +the similarity in name, JavaScript's object model is significantly different to +Java's. + ```js -// Comments are like C's. Single-line comments start with two slashes, -/* and multiline comments start with slash-star +// Single-line comments start with two slashes. +/* Multiline comments start with slash-star, and end with star-slash */ // Statements can be terminated by ; @@ -145,7 +150,7 @@ someOtherVar = 10; // Variables declared without being assigned to are set to undefined. var someThirdVar; // = undefined -// If you want to declare a couple of variables, then you could use a comma +// If you want to declare a couple of variables, then you could use a comma // separator var someFourthVar = 2, someFifthVar = 4; @@ -194,8 +199,6 @@ myObj.myFourthKey; // = undefined /////////////////////////////////// // 3. Logic and Control Structures -// The syntax for this section is almost identical to Java's. - // The `if` structure works as you'd expect. var count = 1; if (count == 3){ -- cgit v1.2.3 From d4d471ef50fb2e84dc8f1656a7037795bd44a89a Mon Sep 17 00:00:00 2001 From: Adam Brenecki Date: Wed, 28 Oct 2015 13:27:48 +1030 Subject: [javascript/en] Formatting 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 3f9eb641..b5c3a3c8 100644 --- a/javascript.html.markdown +++ b/javascript.html.markdown @@ -41,7 +41,7 @@ doStuff() // JavaScript has one number type (which is a 64-bit IEEE 754 double). // Doubles have a 52-bit mantissa, which is enough to store integers -// up to about 9✕10¹⁵ precisely. +// up to about 9✕10¹⁵ precisely. 3; // = 3 1.5; // = 1.5 -- cgit v1.2.3 From c3a66e60a61de0b98ea3e86568dfddf76eae1069 Mon Sep 17 00:00:00 2001 From: Adam Brenecki Date: Wed, 28 Oct 2015 13:48:53 +1030 Subject: [javascript/en] Re-add note about truthiness of wrapped primitives Previous incarnations of this section had `Number(0)` instead of `new Number(0)`, which actually returns `0` due to the absence of the `new` keyword; this commit re-adds that section and better still, makes it actually correct! --- 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 b5c3a3c8..cd75b0d2 100644 --- a/javascript.html.markdown +++ b/javascript.html.markdown @@ -507,6 +507,10 @@ myNumber === myNumberObj; // = false if (0){ // This code won't execute, because 0 is falsy. } +if (new Number(0)){ + // This code will execute, because wrapped numbers are objects, and objects + // are always truthy. +} // However, the wrapper objects and the regular builtins share a prototype, so // you can actually add functionality to a string, for instance. -- cgit v1.2.3 From 08e6aa4e6e0344082517ad94d29fb33b81e827a9 Mon Sep 17 00:00:00 2001 From: Kyle Mendes Date: Wed, 28 Oct 2015 17:25:44 -0400 Subject: Adding a small blurb to extend upon string concatination --- 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 cd75b0d2..e285ca4e 100644 --- a/javascript.html.markdown +++ b/javascript.html.markdown @@ -101,6 +101,10 @@ false; // Strings are concatenated with + "Hello " + "world!"; // = "Hello world!" +// ... which works with more than just strings +"1, 2, " + 3; // = "1, 2, 3" +"Hello " + ["world", "!"] // = "Hello world,!" + // and are compared with < and > "a" < "b"; // = true -- cgit v1.2.3 From 6e9e1f4af018dc0e277c968d9b5fe011e0a1ce97 Mon Sep 17 00:00:00 2001 From: Ryan Date: Sat, 31 Oct 2015 16:49:57 -0700 Subject: Added new resource to javascript --- javascript.html.markdown | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'javascript.html.markdown') diff --git a/javascript.html.markdown b/javascript.html.markdown index e285ca4e..98261334 100644 --- a/javascript.html.markdown +++ b/javascript.html.markdown @@ -561,7 +561,9 @@ of the language. [Eloquent Javascript][8] by Marijn Haverbeke is an excellent JS book/ebook with attached terminal -[Javascript: The Right Way][9] is a guide intended to introduce new developers to JavaScript and help experienced developers learn more about its best practices. +[Eloquent Javascript - The Annotated Version][9] by Gordon Zhu is also a great derivative of Eloquent Javascript with extra explanations and clarifications for some of the more complicated examples. + +[Javascript: The Right Way][10] is a guide intended to introduce new developers to JavaScript and help experienced developers learn more about its best practices. In addition to direct contributors to this article, some content is adapted from @@ -577,4 +579,5 @@ Mozilla Developer Network. [6]: http://www.amazon.com/gp/product/0596805527/ [7]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript [8]: http://eloquentjavascript.net/ -[9]: http://jstherightway.org/ +[9]: http://watchandcode.com/courses/eloquent-javascript-the-annotated-version +[10]: http://jstherightway.org/ -- cgit v1.2.3 From 6d351553ac83f08d8ad784c57740ddc43899859c Mon Sep 17 00:00:00 2001 From: Yi Hong Ang Date: Mon, 31 Oct 2016 07:33:22 +0800 Subject: add labeled loop breaking for Javascript (#2539) --- javascript.html.markdown | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'javascript.html.markdown') diff --git a/javascript.html.markdown b/javascript.html.markdown index 98261334..38119864 100644 --- a/javascript.html.markdown +++ b/javascript.html.markdown @@ -230,6 +230,17 @@ for (var i = 0; i < 5; i++){ // will run 5 times } +// Breaking out of labeled loops is similar to Java +outer: +for (var i = 0; i < 10; i++) { + for (var j = 0; j < 10; j++) { + if (i == 5 && j ==5) { + break outer; + // breaks out of outer loop instead of only the inner one + } + } +} + // The for/in statement iterates over every property across the entire prototype chain. var description = ""; var person = {fname:"Paul", lname:"Ken", age:18}; -- cgit v1.2.3 From 21652477c23e887a6780d6a9dd97e6e15e1a7a2e Mon Sep 17 00:00:00 2001 From: Ravi Subramanian Date: Thu, 23 Feb 2017 16:04:42 -0800 Subject: [javascript/en] Clarify objects, remove premature reference to prototypes - Prototypes are referenced in Section 3 before being discussed (in Section 5) - Also, prototypes are referenced while talking about for-loops, which IMHO distracts from the discussion of the loop structure itself - Added example that shows prototype-chain-walking via for-in in Section 5 --- javascript.html.markdown | 64 +++++++++++++++++++++++++++++++++++------------- 1 file changed, 47 insertions(+), 17 deletions(-) (limited to 'javascript.html.markdown') diff --git a/javascript.html.markdown b/javascript.html.markdown index 38119864..13ed2fb8 100644 --- a/javascript.html.markdown +++ b/javascript.html.markdown @@ -138,7 +138,7 @@ undefined; // used to indicate a value is not currently present (although // Note that 0 is falsy and "0" is truthy, even though 0 == "0". /////////////////////////////////// -// 2. Variables, Arrays and Objects +// 2. Variables, Arrays and basics of Objects // Variables are declared with the `var` keyword. JavaScript is dynamically // typed, so you don't need to specify type. Assignment uses a single `=` @@ -200,6 +200,11 @@ myObj.myThirdKey = true; // If you try to access a value that's not yet set, you'll get undefined. myObj.myFourthKey; // = undefined +// JavaScript objects are not instantiated from blueprints ("classes" in other +// object-oriented languages), but may refer to another special object called a +// "prototype". See later section for more info. + + /////////////////////////////////// // 3. Logic and Control Structures @@ -225,7 +230,7 @@ do { } while (!isValid(input)) // The `for` loop is the same as C and Java: -// initialisation; continue condition; iteration. +// initialization; continue condition; iteration. for (var i = 0; i < 5; i++){ // will run 5 times } @@ -241,22 +246,25 @@ for (var i = 0; i < 10; i++) { } } -// The for/in statement iterates over every property across the entire prototype chain. +// The for/in statement allows iteration over properties of an object. var description = ""; var person = {fname:"Paul", lname:"Ken", age:18}; for (var x in person){ description += person[x] + " "; -} +} // description = 'Paul Ken 18 ' -// To only consider properties attached to the object itself -// and not its prototypes, use the `hasOwnProperty()` check. -var description = ""; -var person = {fname:"Paul", lname:"Ken", age:18}; -for (var x in person){ - if (person.hasOwnProperty(x)){ - description += person[x] + " "; - } +// It also works for array objects but iterates over the indices +var myArray = [1, 2, 3, "apple", "banana", function(){}] +for (var index in myArray) { + console.log(index + ": " + myArray[index]); } +///prints: +// 0: 1 +// 1: 2 +// 2: 3 +// 3: apple +// 4: banana +// 5: function () {} // For/in should not be used to iterate over an Array where the index order // is important, as there is no guarantee that for/in will return the indexes @@ -273,7 +281,6 @@ if (colour == "red" || colour == "blue"){ // && and || "short circuit", which is useful for setting default values. 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. @@ -485,6 +492,26 @@ myObj.myBoolean; // = true myPrototype.meaningOfLife = 43; myObj.meaningOfLife; // = 43 +// The for/in statement allows iteration over properties of an object, +// walking up the prototype chain until it sees a null prototype. +for (var x in myObj){ + console.log(myObj[x]); +} +///prints: +// Hello world! +// 42 +// [Function: myFunc] + +// To only consider properties attached to the object itself +// and not its prototypes, use the `hasOwnProperty()` check. +for (var x in myObj){ + if (myObj.hasOwnProperty(x)){ + console.log(myObj[x]); + } +} +///prints: +// Hello world! + // We mentioned that `__proto__` was non-standard, and there's no standard way to // change the prototype of an existing object. However, there are two ways to // create a new object with a given prototype. @@ -570,12 +597,15 @@ of the language. [JavaScript: The Definitive Guide][6] is a classic guide and reference book. -[Eloquent Javascript][8] by Marijn Haverbeke is an excellent JS book/ebook with attached terminal - -[Eloquent Javascript - The Annotated Version][9] by Gordon Zhu is also a great derivative of Eloquent Javascript with extra explanations and clarifications for some of the more complicated examples. +[Eloquent Javascript][8] by Marijn Haverbeke is an excellent JS book/ebook with +attached terminal -[Javascript: The Right Way][10] is a guide intended to introduce new developers to JavaScript and help experienced developers learn more about its best practices. +[Eloquent Javascript - The Annotated Version][9] by Gordon Zhu is also a great +derivative of Eloquent Javascript with extra explanations and clarifications for +some of the more complicated examples. +[Javascript: The Right Way][10] is a guide intended to introduce new developers +to JavaScript and help experienced developers learn more about its best practices. In addition to direct contributors to this article, some content is adapted from Louie Dinh's Python tutorial on this site, and the [JS Tutorial][7] on the -- cgit v1.2.3 From 54eff5591a1701b40af8632b6c9239dbb8a68ddc Mon Sep 17 00:00:00 2001 From: Adam Brenecki Date: Wed, 1 Mar 2017 19:29:32 +1030 Subject: [javascript/en] Minor changes to formatting, prototype explanations - Made a comment a complete sentence, to match the style of the rest of the document. - Removed discussion of for/in over Arrays, since it's a can of worms, and for/of is more appropriate if ES6 operators are available. - Reworded introduction to prototypes, and moved it with the rest of the prototype documentation. --- javascript.html.markdown | 28 +++++----------------------- 1 file changed, 5 insertions(+), 23 deletions(-) (limited to 'javascript.html.markdown') diff --git a/javascript.html.markdown b/javascript.html.markdown index 13ed2fb8..a85a7872 100644 --- a/javascript.html.markdown +++ b/javascript.html.markdown @@ -138,7 +138,7 @@ undefined; // used to indicate a value is not currently present (although // Note that 0 is falsy and "0" is truthy, even though 0 == "0". /////////////////////////////////// -// 2. Variables, Arrays and basics of Objects +// 2. Variables, Arrays and Objects // Variables are declared with the `var` keyword. JavaScript is dynamically // typed, so you don't need to specify type. Assignment uses a single `=` @@ -200,11 +200,6 @@ myObj.myThirdKey = true; // If you try to access a value that's not yet set, you'll get undefined. myObj.myFourthKey; // = undefined -// JavaScript objects are not instantiated from blueprints ("classes" in other -// object-oriented languages), but may refer to another special object called a -// "prototype". See later section for more info. - - /////////////////////////////////// // 3. Logic and Control Structures @@ -253,23 +248,6 @@ for (var x in person){ description += person[x] + " "; } // description = 'Paul Ken 18 ' -// It also works for array objects but iterates over the indices -var myArray = [1, 2, 3, "apple", "banana", function(){}] -for (var index in myArray) { - console.log(index + ": " + myArray[index]); -} -///prints: -// 0: 1 -// 1: 2 -// 2: 3 -// 3: apple -// 4: banana -// 5: function () {} - -// For/in should not be used to iterate over an Array where the index order -// is important, as 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"; @@ -456,6 +434,10 @@ var MyConstructor = function(){ myNewObj = new MyConstructor(); // = {myNumber: 5} myNewObj.myNumber; // = 5 +// Unlike most other popular object-oriented languages, JavaScript has no +// concept of 'instances' created from 'class' blueprints; instead, JavaScript +// combines instantiation and inheritance into a single concept: a 'prototype'. + // Every JavaScript object has a 'prototype'. When you go to access a property // on an object that doesn't exist on the actual object, the interpreter will // look at its prototype. -- cgit v1.2.3 From 966886c6a9bd651679d6846f25d75666dfcbfb3c Mon Sep 17 00:00:00 2001 From: rlgreen91 Date: Thu, 8 Jun 2017 16:35:17 -0500 Subject: Corrects typo in prototype section. --- 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 a85a7872..85c8a52d 100644 --- a/javascript.html.markdown +++ b/javascript.html.markdown @@ -481,7 +481,7 @@ for (var x in myObj){ } ///prints: // Hello world! -// 42 +// 43 // [Function: myFunc] // To only consider properties attached to the object itself -- cgit v1.2.3 From 575dc7c0184978f73c4327c5f27f1c41c090fa5a Mon Sep 17 00:00:00 2001 From: Kyle Mendes Date: Fri, 27 Oct 2017 16:09:35 -0400 Subject: Adding missing semicolons --- javascript.html.markdown | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) (limited to 'javascript.html.markdown') diff --git a/javascript.html.markdown b/javascript.html.markdown index 85c8a52d..4ed8f849 100644 --- a/javascript.html.markdown +++ b/javascript.html.markdown @@ -103,7 +103,7 @@ false; // ... which works with more than just strings "1, 2, " + 3; // = "1, 2, 3" -"Hello " + ["world", "!"] // = "Hello world,!" +"Hello " + ["world", "!"]; // = "Hello world,!" // and are compared with < and > "a" < "b"; // = true @@ -222,7 +222,7 @@ while (true){ var input; do { input = getInput(); -} while (!isValid(input)) +} while (!isValid(input)); // The `for` loop is the same as C and Java: // initialization; continue condition; iteration. @@ -293,7 +293,7 @@ myFunction("foo"); // = "FOO" // automatic semicolon insertion. Watch out for this when using Allman style. function myFunction(){ return // <- semicolon automatically inserted here - {thisIsAn: 'object literal'} + {thisIsAn: 'object literal'}; } myFunction(); // = undefined @@ -388,7 +388,7 @@ myFunc(); // = undefined // through `this`, even if it wasn't attached when it was defined. var myOtherFunc = function(){ return this.myString.toUpperCase(); -} +}; myObj.myOtherFunc = myOtherFunc; myObj.myOtherFunc(); // = "HELLO WORLD!" @@ -397,7 +397,7 @@ myObj.myOtherFunc(); // = "HELLO WORLD!" var anotherFunc = function(s){ return this.myString + s; -} +}; anotherFunc.call(myObj, " And Hello Moon!"); // = "Hello World! And Hello Moon!" // The `apply` function is nearly identical, but takes an array for an argument @@ -420,7 +420,7 @@ boundFunc(" And Hello Saturn!"); // = "Hello World! And Hello Saturn!" // `bind` can also be used to partially apply (curry) a function. -var product = function(a, b){ return a * b; } +var product = function(a, b){ return a * b; }; var doubler = product.bind(this, 2); doubler(8); // = 16 @@ -430,11 +430,11 @@ doubler(8); // = 16 var MyConstructor = function(){ this.myNumber = 5; -} +}; myNewObj = new MyConstructor(); // = {myNumber: 5} myNewObj.myNumber; // = 5 -// Unlike most other popular object-oriented languages, JavaScript has no +// Unlike most other popular object-oriented languages, JavaScript has no // concept of 'instances' created from 'class' blueprints; instead, JavaScript // combines instantiation and inheritance into a single concept: a 'prototype'. @@ -451,7 +451,7 @@ var myObj = { var myPrototype = { meaningOfLife: 42, myFunc: function(){ - return this.myString.toLowerCase() + return this.myString.toLowerCase(); } }; @@ -515,7 +515,7 @@ MyConstructor.prototype = { }; var myNewObj2 = new MyConstructor(); myNewObj2.getMyNumber(); // = 5 -myNewObj2.myNumber = 6 +myNewObj2.myNumber = 6; myNewObj2.getMyNumber(); // = 6 // Built-in types like strings and numbers also have constructors that create @@ -540,7 +540,7 @@ if (new Number(0)){ // you can actually add functionality to a string, for instance. String.prototype.firstCharacter = function(){ return this.charAt(0); -} +}; "abc".firstCharacter(); // = "a" // This fact is often used in "polyfilling", which is implementing newer @@ -556,7 +556,7 @@ if (Object.create === undefined){ // don't overwrite it if it exists Constructor.prototype = proto; // then use it to create a new, appropriately-prototyped object return new Constructor(); - } + }; } ``` -- cgit v1.2.3 From cbb09c2df6fe9b373457c3b59380038e85f00e92 Mon Sep 17 00:00:00 2001 From: name Date: Tue, 31 Oct 2017 13:57:42 +0300 Subject: [javascript/en] Add several usefull functions to work with arrays --- javascript.html.markdown | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'javascript.html.markdown') diff --git a/javascript.html.markdown b/javascript.html.markdown index 4ed8f849..75bdf1ad 100644 --- a/javascript.html.markdown +++ b/javascript.html.markdown @@ -180,6 +180,23 @@ myArray.length; // = 4 // Add/Modify at specific index myArray[3] = "Hello"; +// Add and remove element from front or back end of an array +myArray.unshift(3); // Add as the first element +someVar = myArray.shift(); // Remove first element and return it +myArray.push(3); // Add as the last element +someVar = myArray.pop(); // Remove last element and return it + +// Join all elements of an array with a string +var myArray0 = [32,false,"js",12,56,90]; +myArray0.join(";") // = "32;false;js;12;56;90" + +// Get subarray of elements from index 1 (include) to 4 (exclude) +myArray0.slice(1,4); // = [false,"js",12] + +// Remove 4 elements starting from index 2, and insert there strings +// "33","34" and "35"; return removed subarray +myArray0.splice(2,4,"33","34","35"); + // JavaScript's objects are equivalent to "dictionaries" or "maps" in other // languages: an unordered collection of key-value pairs. var myObj = {key1: "Hello", key2: "World"}; -- cgit v1.2.3 From aff623e234d8d0844a44ff5331d756ae539aa6d0 Mon Sep 17 00:00:00 2001 From: name Date: Mon, 13 Nov 2017 00:41:52 +0300 Subject: Comments improved --- javascript.html.markdown | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'javascript.html.markdown') diff --git a/javascript.html.markdown b/javascript.html.markdown index 75bdf1ad..199c3c09 100644 --- a/javascript.html.markdown +++ b/javascript.html.markdown @@ -186,7 +186,7 @@ someVar = myArray.shift(); // Remove first element and return it myArray.push(3); // Add as the last element someVar = myArray.pop(); // Remove last element and return it -// Join all elements of an array with a string +// Join all elements of an array with semicolon var myArray0 = [32,false,"js",12,56,90]; myArray0.join(";") // = "32;false;js;12;56;90" @@ -194,8 +194,9 @@ myArray0.join(";") // = "32;false;js;12;56;90" myArray0.slice(1,4); // = [false,"js",12] // Remove 4 elements starting from index 2, and insert there strings -// "33","34" and "35"; return removed subarray -myArray0.splice(2,4,"33","34","35"); +// "hi","wr" and "ld"; return removed subarray +myArray0.splice(2,4,"hi","wr","ld"); // = ["js",12,56,90] +// myArray0 === [32,false,"hi","wr","ld"] // JavaScript's objects are equivalent to "dictionaries" or "maps" in other // languages: an unordered collection of key-value pairs. -- cgit v1.2.3 From c06af7a5ac4097b26113ea649893e57e405edefc Mon Sep 17 00:00:00 2001 From: Apoorv Choubey Date: Sun, 17 Dec 2017 15:56:42 +0530 Subject: Added a resource to read for Javascript --- javascript.html.markdown | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'javascript.html.markdown') diff --git a/javascript.html.markdown b/javascript.html.markdown index 199c3c09..e7066291 100644 --- a/javascript.html.markdown +++ b/javascript.html.markdown @@ -607,6 +607,10 @@ some of the more complicated examples. [Javascript: The Right Way][10] is a guide intended to introduce new developers to JavaScript and help experienced developers learn more about its best practices. +[Javascript:Info][11] is a modern javascript tutorial covering the basics (core language and working with a browser) +as well as advanced topics with concise explanations. + + In addition to direct contributors to this article, some content is adapted from Louie Dinh's Python tutorial on this site, and the [JS Tutorial][7] on the Mozilla Developer Network. @@ -622,3 +626,4 @@ Mozilla Developer Network. [8]: http://eloquentjavascript.net/ [9]: http://watchandcode.com/courses/eloquent-javascript-the-annotated-version [10]: http://jstherightway.org/ +[11]: https://javascript.info/ -- cgit v1.2.3