From a19ba3205dd7bada3a20f2cc10e8199ab1f08a9a Mon Sep 17 00:00:00 2001 From: Adam Brenecki Date: Sat, 29 Jun 2013 19:44:10 +0930 Subject: Start work on the JavaScript tutorial --- javascript.html.markdown | 114 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 javascript.html.markdown diff --git a/javascript.html.markdown b/javascript.html.markdown new file mode 100644 index 00000000..737638b1 --- /dev/null +++ b/javascript.html.markdown @@ -0,0 +1,114 @@ +--- +language: javascript +author: Adam Brenecki +author_url: http://adam.brenecki.id.au +--- + +Javascript was created by Netscape's Brendan Eich in 1995. It was originally +intended as a simpler scripting language for web apps, complimenting Java for +more complex ones, but has become far more widely used than Java on the web. + +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). + +```javascript +// Comments are like C. Single-line comments start with two slashes, +/* and multiline comments start with slash-star + and end with star-slash */ + +// Statements can be terminated by ; +doStuff(); + +// ... but they don't have to be, as semicolons are automatically inserted +// wherever there's a newline, except in certain cases. +doStuff() + +// Semicolons are a heated topic in the JavaScript world, but they're really a +// matter of personal or style-guide preference. We'll leave them off here. + +/*********** + * 1. Primitive Datatypes and Operators + ***********/ + +// Javascript has one number type that covers ints and floats. +3 // = 3 +1.5 // = 1.5 + +// which support all the operations you'd expect. +1 + 1 // = 2 +8 - 1 // = 7 +10 * 2 // = 20 +35 / 5 // = 7 + +// Uneven division works how you'd expect, too. +5 / 2 # = 2.5 + +// Enforce precedence with parentheses +(1 + 3) * 2 // = 8 + +// There's also a boolean type. +true +false + +// Strings are created with ' or ". +'abc' +"Hello, world" + +// Negation uses the ! symbol +!true // = false +!false // = true + +// Equality is == +1 == 1 // = true +2 == 1 // = false + +// Inequality is != +1 != 1 // = false +2 != 1 // = true + +// More comparisons +1 < 10 #=> True +1 > 10 #=> False +2 <= 2 #=> True +2 >= 2 #=> True + +// Strings are concatenated with + +"Hello " + "world!" // = "Hello world!" + +// and are compared with < and > +"a" < "b" // = true + +// You can also compare strings with numbers +"5" == 5 // = true + +// but this is almost always not what you want, so use === to stop this +"5" === 5 // = false + +// You can access characters in a string with charAt +"This is a string".charAt(0) + +// There's also a null keyword +null // = null + +/*********** + * 2. Variables and Lists + ***********/ + +// variables are declared with the var keyword +var some_var = 5 + +// if you leave them off, you won't get an error... +some_other_var = 10 + +// but your variable will always end up with the global scope, even if it wasn't +// defined there, so don't do it. + +/*********** + * 3. Control Structures + ***********/ + +/*********** + * 4. Objects + ***********/ +``` -- cgit v1.2.3 From a8672f19bfe97c0f9157d7ff1050913df75c976e Mon Sep 17 00:00:00 2001 From: Adam Brenecki Date: Sat, 29 Jun 2013 19:58:54 +0930 Subject: Add the rest of the tutorial's structure --- javascript.html.markdown | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/javascript.html.markdown b/javascript.html.markdown index 737638b1..826fe7cd 100644 --- a/javascript.html.markdown +++ b/javascript.html.markdown @@ -111,4 +111,12 @@ some_other_var = 10 /*********** * 4. Objects ***********/ + +/*********** + * 5. Functions, Scope and Closures + ***********/ + +/*********** + * 6. Constructors and Prototypes + ***********/ ``` -- cgit v1.2.3 From 9c81beb8114d246cd0de5e83340363cdebc87155 Mon Sep 17 00:00:00 2001 From: Adam Brenecki Date: Sat, 29 Jun 2013 21:09:19 +0930 Subject: Begin writing about JavaScript arrays and dictionaries --- javascript.html.markdown | 121 +++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 113 insertions(+), 8 deletions(-) diff --git a/javascript.html.markdown b/javascript.html.markdown index 826fe7cd..1565f541 100644 --- a/javascript.html.markdown +++ b/javascript.html.markdown @@ -92,24 +92,41 @@ false null // = null /*********** - * 2. Variables and Lists + * 2. Variables, Arrays and Objects ***********/ -// variables are declared with the var keyword +// 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 some_var = 5 -// if you leave them off, you won't get an error... +// if you leave the var keyword off, you won't get an error... some_other_var = 10 // but your variable will always end up with the global scope, even if it wasn't // defined there, so don't do it. -/*********** - * 3. Control Structures - ***********/ +// Arrays are ordered lists of values, of any type. +["Hello", 45, true] + +// JavaScript's objects are equivalent to 'dictionaries' or 'maps' in other +// languages: an unordered collection of key-value pairs. +{key1: "Hello", key2: "World"} + +// Keys are strings, but quotes aren't required if they're a valid +// JavaScript identifier. Values can be any type. +var myObj = {myKey: "myValue", "my other key": 4} + +// Object attributes can be accessed using the 'subscript' syntax, +myObj["my other key"] // = 4 + +// ... or using the dot syntax, provided the key is a valid identifier. +myObj.myKey // = "myValue" + +// Objects are mutable, values can be changed and new keys added. +myObj.myThirdKey = true /*********** - * 4. Objects + * 3. Control Structures ***********/ /*********** @@ -117,6 +134,94 @@ some_other_var = 10 ***********/ /*********** - * 6. Constructors and Prototypes + * 6. More about Objects; Constructors and Prototypes ***********/ + +// Objects can contain functions, which can be called using the dot syntax. +myObj = { + myFunc: function(){ + return "Hello world!" + } +} +myObj.myFunc() // = "Hello world!" + +// When functions are called like this, they can access the object they're +// attached to using the this keyword. +myObj = { + myString: "Hello world!", + myFunc: function(){ + return this.myString + } +} +myObj.myFunc() // = "Hello world!" + +// The value of this has to do with how the function is called, not where it's +// defined. So, that doesn't work if the function isn't called in the context of +// the object. +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 defined as such. +var myOtherFunc = function(){ + return this.myString.toUpperCase() +} +myObj.myOtherFunc = myOtherFunc +myObj.myOtherFunc() // = "HELLO WORLD!" + +// When you call a function with the new keyword, a new object is created, and +// made available to the function via this. Functions designed to be called +// like this are called constructors. + +var MyConstructor = function(){ + this.myNumber = 5 +} +myNewObj = new MyConstructor() // = {myNumber: 5} +myNewObj.myNumber // = 5 + +// JavaScript objects aren't defined in terms of classes like other languages, +// but you can use prototypes to do many of the same things. When you try to +// access a property of an object that isn't present, its prototype is searched. +var myObj = {} +var myPrototype = { + meaningOfLife: 42, + myThirdFunc: function(){ + return this.myString.toLowerCase() + } +} +myObj.__proto__ = myPrototype +myObj.myThirdFunc() // = "hello world!" + +// Of course, if your property isn't on your prototype, the prototype's +// prototype is searched, and so on. +myPrototype.__proto__ = { + myBoolean: true +} +myObj.myBoolean // = true + +// There's no copying involved here; each object stores a reference to its +// prototype. This means we can alter the prototype and our changes will be +// reflected everywhere. +myObj. + +// The __proto__ magic property we've used to access prototypes isn't standard, +// and shouldn't be used in real-world code. There is a way to create a new +// object with another given object as its prototype, though: +var myObj = Object.create(myPrototype) +myObj.meaningOfLife // = 42 + +// Unfortunately, Object.create is quite recent and isn't available in many +// browsers, so you often can't use that, either. The most reliable way to set +// prototypes involves constructors. + +// TODO: write about the .prototype property on constructors + +// Built-in types' prototypes work like this too, so you can actually change +// the prototype of a string, for instance (although whether you should is +// another matter). +String.prototype.firstCharacter = function(){ + return this.charAt(0) +} +"abc".firstCharacter() // = "a" + ``` -- cgit v1.2.3 From 325ecf20a02655643c22e9692d697d258211814e Mon Sep 17 00:00:00 2001 From: DrJonOsterman Date: Sat, 29 Jun 2013 08:46:34 -0400 Subject: comment syntax fix --- javascript.html.markdown | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/javascript.html.markdown b/javascript.html.markdown index 1565f541..c3bf131c 100644 --- a/javascript.html.markdown +++ b/javascript.html.markdown @@ -42,7 +42,7 @@ doStuff() 35 / 5 // = 7 // Uneven division works how you'd expect, too. -5 / 2 # = 2.5 +5 / 2 // = 2.5 // Enforce precedence with parentheses (1 + 3) * 2 // = 8 @@ -68,10 +68,10 @@ false 2 != 1 // = true // More comparisons -1 < 10 #=> True -1 > 10 #=> False -2 <= 2 #=> True -2 >= 2 #=> True +1 < 10 // => True +1 > 10 // => False +2 <= 2 // => True +2 >= 2 // => True // Strings are concatenated with + "Hello " + "world!" // = "Hello world!" -- cgit v1.2.3 From 600b5b5b741f6a8937b20f8f154362a125ce4e92 Mon Sep 17 00:00:00 2001 From: Adam Brenecki Date: Sun, 30 Jun 2013 17:33:10 +0930 Subject: Added links to MDN, tidied intro --- javascript.html.markdown | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/javascript.html.markdown b/javascript.html.markdown index c3bf131c..acac1795 100644 --- a/javascript.html.markdown +++ b/javascript.html.markdown @@ -5,8 +5,10 @@ author_url: http://adam.brenecki.id.au --- Javascript was created by Netscape's Brendan Eich in 1995. It was originally -intended as a simpler scripting language for web apps, complimenting Java for -more complex ones, but has become far more widely used than Java on the web. +intended as a simpler scripting language for websites, complimenting the use of +Java for more complex web applications, but its tight integration with Web pages +and built-in support in browsers has caused it to become far more common than +Java in web frontends. Feedback would be highly appreciated! You can reach me at [@adambrenecki](https://twitter.com/adambrenecki), or @@ -225,3 +227,11 @@ String.prototype.firstCharacter = function(){ "abc".firstCharacter() // = "a" ``` + +## 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. -- cgit v1.2.3 From 7c4bd7120c36b525c126e08df0b22c25830ba4e0 Mon Sep 17 00:00:00 2001 From: Adam Brenecki Date: Sun, 30 Jun 2013 17:44:25 +0930 Subject: Edits to section on strings and numbers --- javascript.html.markdown | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/javascript.html.markdown b/javascript.html.markdown index acac1795..6234aebc 100644 --- a/javascript.html.markdown +++ b/javascript.html.markdown @@ -26,29 +26,34 @@ doStuff(); // wherever there's a newline, except in certain cases. doStuff() -// Semicolons are a heated topic in the JavaScript world, but they're really a -// matter of personal or style-guide preference. We'll leave them off here. +// We'll leave semicolons off here; whether you do or not will depend on your +// personal preference or your project's style guide. /*********** - * 1. Primitive Datatypes and Operators + * 1. Numbers, Strings and Operators ***********/ // Javascript has one number type that covers ints and floats. 3 // = 3 1.5 // = 1.5 -// which support all the operations you'd expect. +// All the basic arithmetic works as you'd expect. 1 + 1 // = 2 8 - 1 // = 7 10 * 2 // = 20 35 / 5 // = 7 -// Uneven division works how you'd expect, too. +// Including uneven division. 5 / 2 // = 2.5 // Enforce precedence with parentheses (1 + 3) * 2 // = 8 +// 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 + // There's also a boolean type. true false @@ -70,10 +75,10 @@ false 2 != 1 // = true // More comparisons -1 < 10 // => True -1 > 10 // => False -2 <= 2 // => True -2 >= 2 // => True +1 < 10 // = true +1 > 10 // = false +2 <= 2 // = true +2 >= 2 // = true // Strings are concatenated with + "Hello " + "world!" // = "Hello world!" @@ -81,17 +86,21 @@ false // and are compared with < and > "a" < "b" // = true -// You can also compare strings with numbers +// Type coercion is performed for comparisons... "5" == 5 // = true -// but this is almost always not what you want, so use === to stop this +// ...unless you use === "5" === 5 // = false // You can access characters in a string with charAt "This is a string".charAt(0) -// There's also a null keyword -null // = null +// There's also null and undefined +null // used to indicate a deliberate non-value +undefined // used to indicate a value that hasn't been set yet + +// null, undefined, NaN, 0 and "" are falsy, and everything else is truthy. +// Note that 0 is falsy and "0" is truthy, even though 0 == "0". /*********** * 2. Variables, Arrays and Objects -- cgit v1.2.3 From c2d5429472fb945bec81b42789dc0fd6161df433 Mon Sep 17 00:00:00 2001 From: Adam Brenecki Date: Sun, 30 Jun 2013 17:48:50 +0930 Subject: Tidy up section on variables, arrays and objects --- javascript.html.markdown | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/javascript.html.markdown b/javascript.html.markdown index 6234aebc..cb866886 100644 --- a/javascript.html.markdown +++ b/javascript.html.markdown @@ -108,13 +108,16 @@ undefined // used to indicate a value that hasn't been set yet // 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 some_var = 5 +var someVar = 5 // if you leave the var keyword off, you won't get an error... -some_other_var = 10 +someOtherVar = 10 -// but your variable will always end up with the global scope, even if it wasn't -// defined there, so don't do it. +// ...but your variable will be created in the global scope, not in the scope +// you defined it in. + +// Variables declared without being assigned to are set to undefined. +var someThirdVar // = undefined // Arrays are ordered lists of values, of any type. ["Hello", 45, true] @@ -133,7 +136,7 @@ myObj["my other key"] // = 4 // ... or using the dot syntax, provided the key is a valid identifier. myObj.myKey // = "myValue" -// Objects are mutable, values can be changed and new keys added. +// Objects are mutable; values can be changed and new keys added. myObj.myThirdKey = true /*********** -- cgit v1.2.3 From 3b8ece99323979cbb598589ec062b56fa311ad2c Mon Sep 17 00:00:00 2001 From: Adam Brenecki Date: Sun, 30 Jun 2013 18:14:53 +0930 Subject: Continue work on objects cont'd section --- javascript.html.markdown | 73 ++++++++++++++++++++++++++++++++++-------------- 1 file changed, 52 insertions(+), 21 deletions(-) diff --git a/javascript.html.markdown b/javascript.html.markdown index cb866886..afa37bf1 100644 --- a/javascript.html.markdown +++ b/javascript.html.markdown @@ -139,6 +139,9 @@ myObj.myKey // = "myValue" // Objects are mutable; values can be changed and new keys added. myObj.myThirdKey = true +// If you try to access a value that's not yet set, you'll get undefined. +myObj.myFourthKey // = undefined + /*********** * 3. Control Structures ***********/ @@ -151,16 +154,16 @@ myObj.myThirdKey = true * 6. More about Objects; Constructors and Prototypes ***********/ -// Objects can contain functions, which can be called using the dot syntax. -myObj = { +// Objects can contain functions. +var myObj = { myFunc: function(){ return "Hello world!" } } myObj.myFunc() // = "Hello world!" -// When functions are called like this, they can access the object they're -// attached to using the this keyword. +// When functions attached to an object are called, they can access the object +// they're attached to using the this keyword. myObj = { myString: "Hello world!", myFunc: function(){ @@ -169,14 +172,14 @@ myObj = { } myObj.myFunc() // = "Hello world!" -// The value of this has to do with how the function is called, not where it's -// defined. So, that doesn't work if the function isn't called in the context of -// the object. +// What this is set to has to do with how the function is called, not where +// it's defined. So, our function doesn't work if it isn't called in the +// context of the object. 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 defined as such. +// through this, even if it wasn't attached when it was defined. var myOtherFunc = function(){ return this.myString.toUpperCase() } @@ -193,18 +196,27 @@ var MyConstructor = function(){ myNewObj = new MyConstructor() // = {myNumber: 5} myNewObj.myNumber // = 5 -// JavaScript objects aren't defined in terms of classes like other languages, -// but you can use prototypes to do many of the same things. When you try to -// access a property of an object that isn't present, its prototype is searched. -var myObj = {} +// 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. + +// 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 +// part of the standard; we'll get to standard ways of using prototypes later. +var myObj = { + myString: "Hello world!", +} var myPrototype = { meaningOfLife: 42, - myThirdFunc: function(){ + myFunc: function(){ return this.myString.toLowerCase() } } myObj.__proto__ = myPrototype -myObj.myThirdFunc() // = "hello world!" +myObj.meaningOfLife // = 42 + +// This works for functions, too. +myObj.myFunc() // = "hello world!" // Of course, if your property isn't on your prototype, the prototype's // prototype is searched, and so on. @@ -216,13 +228,18 @@ myObj.myBoolean // = true // There's no copying involved here; each object stores a reference to its // prototype. This means we can alter the prototype and our changes will be // reflected everywhere. -myObj. +myPrototype.meaningOfLife = 43 +myObj.meaningOfLife // = 43 + +// While the __proto__ magic property we've seen so far is useful for +// explaining prototypes, it's non-standard. There's no standard way to change +// an existing object's prototype, but there's two ways to set the prototype of +// a new object when you first create it. -// The __proto__ magic property we've used to access prototypes isn't standard, -// and shouldn't be used in real-world code. There is a way to create a new -// object with another given object as its prototype, though: +// The first is Object.create, which is a recent addition to JS, and therefore +// not available in all implementations yet. var myObj = Object.create(myPrototype) -myObj.meaningOfLife // = 42 +myObj.meaningOfLife // = 43 // Unfortunately, Object.create is quite recent and isn't available in many // browsers, so you often can't use that, either. The most reliable way to set @@ -231,13 +248,27 @@ myObj.meaningOfLife // = 42 // TODO: write about the .prototype property on constructors // Built-in types' prototypes work like this too, so you can actually change -// the prototype of a string, for instance (although whether you should is -// another matter). +// the prototype of a string, for instance. String.prototype.firstCharacter = function(){ return this.charAt(0) } "abc".firstCharacter() // = "a" +// There are several implementations of JavaScript, which all gain new features +// at different times. Sometimes, however, it's possible to replicate new +// features by altering built in types or prototypes, which is called +// "polyfilling". + +// For instance, we mentioned that Object.create isn't yet available in all +// implementations, but we can still use it if we do this: +if (Object.create === undefined){ + Object.create = function(proto){ + // make a temporary constructor with the right prototype + var Constructor = function(){} + Constructor.prototype = proto + return new Constructor() + } +} ``` ## Further Reading -- cgit v1.2.3 From 8a870cab449e1aa1dd9741657457b061020a3ae9 Mon Sep 17 00:00:00 2001 From: Adam Brenecki Date: Sun, 30 Jun 2013 18:57:40 +0930 Subject: Add control structures --- javascript.html.markdown | 60 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 57 insertions(+), 3 deletions(-) diff --git a/javascript.html.markdown b/javascript.html.markdown index afa37bf1..858cec52 100644 --- a/javascript.html.markdown +++ b/javascript.html.markdown @@ -119,8 +119,20 @@ someOtherVar = 10 // Variables declared without being assigned to are set to undefined. var someThirdVar // = undefined +// 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 + +// and an even-shorter-hand for adding or subtracting 1 +someVar++ // now someVar is 101 +someVar-- // back to 100 + // Arrays are ordered lists of values, of any type. -["Hello", 45, true] +var myArray = ["Hello", 45, true] + +// Their members can be accessed using the square-brackets subscript syntax. +// Array indices start at zero. +myArray[1] // = 45 // JavaScript's objects are equivalent to 'dictionaries' or 'maps' in other // languages: an unordered collection of key-value pairs. @@ -130,7 +142,7 @@ var someThirdVar // = undefined // JavaScript identifier. Values can be any type. var myObj = {myKey: "myValue", "my other key": 4} -// Object attributes can be accessed using the 'subscript' syntax, +// Object attributes can also be accessed using the subscript syntax, myObj["my other key"] // = 4 // ... or using the dot syntax, provided the key is a valid identifier. @@ -143,9 +155,46 @@ myObj.myThirdKey = true myObj.myFourthKey // = undefined /*********** - * 3. Control Structures + * 3. Logic and Control Structures ***********/ +// The if structure works as you'd expect. +var count = 1 +if (count == 3){ + // evaluated if count is 3 +} else if (count == 4) { + // evaluated if count is 4 +} else { + // evaluated if it's not either +} + +// As does while. +while (true) { + // An infinite loop! +} + +// Do-while loops are like while loops, except they always run at least once. +var input +do { + input = getInput() +} while (!isValid(input)) + +// the for loop is the same as C and Java: initialisation; test; iteration. +for (var i = 0; i < 5; i++){ + // will run 5 times +} + +// && is logical and, || is logical or +if (house.size == "big" && house.colour == "blue"){ + house.contains = "bear" +} +if (colour == "red" || colour == "blue"){ + // colour is either red or blue +} + +// && and || "short circuit", which is useful for setting default values... +var name = otherName || "default"; + /*********** * 5. Functions, Scope and Closures ***********/ @@ -278,3 +327,8 @@ 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. + +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. -- cgit v1.2.3 From 3dfdab742dc52b61c58682664db35c20b737a05c Mon Sep 17 00:00:00 2001 From: Adam Brenecki Date: Wed, 3 Jul 2013 15:21:03 +0930 Subject: Miscellaneous edits to JavaScript --- javascript.html.markdown | 71 +++++++++++++++++++++++++++++++++--------------- 1 file changed, 49 insertions(+), 22 deletions(-) diff --git a/javascript.html.markdown b/javascript.html.markdown index 858cec52..f8dd2ab2 100644 --- a/javascript.html.markdown +++ b/javascript.html.markdown @@ -33,7 +33,7 @@ doStuff() * 1. Numbers, Strings and Operators ***********/ -// Javascript has one number type that covers ints and floats. +// Javascript has one number type, which is a 64-bit IEEE 754 double. 3 // = 3 1.5 // = 1.5 @@ -46,6 +46,10 @@ doStuff() // Including uneven division. 5 / 2 // = 2.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 + // Enforce precedence with parentheses (1 + 3) * 2 // = 8 @@ -165,7 +169,7 @@ if (count == 3){ } else if (count == 4) { // evaluated if count is 4 } else { - // evaluated if it's not either + // evaluated if it's not either 3 or 4 } // As does while. @@ -179,7 +183,8 @@ do { input = getInput() } while (!isValid(input)) -// the for loop is the same as C and Java: initialisation; test; iteration. +// 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 } @@ -192,8 +197,8 @@ if (colour == "red" || colour == "blue"){ // colour is either red or blue } -// && and || "short circuit", which is useful for setting default values... -var name = otherName || "default"; +// && and || "short circuit", which is useful for setting default values. +var name = otherName || "default" /*********** * 5. Functions, Scope and Closures @@ -280,41 +285,63 @@ myObj.myBoolean // = true myPrototype.meaningOfLife = 43 myObj.meaningOfLife // = 43 -// While the __proto__ magic property we've seen so far is useful for -// explaining prototypes, it's non-standard. There's no standard way to change -// an existing object's prototype, but there's two ways to set the prototype of -// a new object when you first create it. +// We mentioned that __proto__ was non-standard, and there's no standard way to +// change the prototype of an existing object. However, there's two ways to +// create a new object with a given prototype. // The first is Object.create, which is a recent addition to JS, and therefore // not available in all implementations yet. var myObj = Object.create(myPrototype) myObj.meaningOfLife // = 43 -// Unfortunately, Object.create is quite recent and isn't available in many -// browsers, so you often can't use that, either. The most reliable way to set -// prototypes involves constructors. - -// TODO: write about the .prototype property on constructors +// The second way, which works anywhere, has to do with constructors. +// Constructors have a property called prototype. This is *not* the prototype of +// the constructor function itself; instead, it's the prototype that new objects +// are given when they're created with that constructor and the new keyword. +myConstructor.prototype = { + getMyNumber: function(){ + return self.myNumber + } +} +var myNewObj2 = new myConstructor() +myNewObj2.getMyNumber() // = 5 + +// Built-in types like strings and numbers also have constructors that create +// equivalent wrapper objects. +var myNumber = 12 +var myNumberObj = new Number(12) +myNumber == myNumberObj // = true + +// Except, they aren't exactly equivalent. +typeof(myNumber) // = 'number' +typeof(myNumberObj) // = 'object' +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. +} -// Built-in types' prototypes work like this too, so you can actually change -// the prototype of a string, for instance. +// However, the wrapper objects and the regular builtins share a prototype, so +// you can actually add functionality to a string, for instance. String.prototype.firstCharacter = function(){ return this.charAt(0) } "abc".firstCharacter() // = "a" -// There are several implementations of JavaScript, which all gain new features -// at different times. Sometimes, however, it's possible to replicate new -// features by altering built in types or prototypes, which is called -// "polyfilling". +// This fact is often used in "polyfilling", which is implementing newer +// features of JavaScript in an older subset of JavaScript, so that they can be +// used in older environments such as outdated browsers. // For instance, we mentioned that Object.create isn't yet available in all -// implementations, but we can still use it if we do this: -if (Object.create === undefined){ +// implementations, but we can still use it with this polyfill: +if (Object.create === undefined){ // don't overwrite it if it exists Object.create = function(proto){ // make a temporary constructor with the right prototype var Constructor = function(){} Constructor.prototype = proto + // then use it to create a new, appropriately-prototyped object return new Constructor() } } -- cgit v1.2.3 From 80c16770cdfadb84be0c08dbc4e2a7ff7e274745 Mon Sep 17 00:00:00 2001 From: Adam Brenecki Date: Thu, 4 Jul 2013 19:49:24 +0930 Subject: Added 'functions, scope and closures' section --- javascript.html.markdown | 65 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/javascript.html.markdown b/javascript.html.markdown index f8dd2ab2..bdb386f7 100644 --- a/javascript.html.markdown +++ b/javascript.html.markdown @@ -204,6 +204,71 @@ var name = otherName || "default" * 5. Functions, Scope and Closures ***********/ +// JavaScript functions are declared with the function keyword. +function myFunction(thing){ + return thing.toUpperCase() +} +myFunction("foo") // = "FOO" + +// Functions can also be defined "anonymously" - without a name: +function(thing){ + return thing.toLowerCase() +} +// (we can't call our function, since we don't have a name to refer to it with) + +// JavaScript functions are first class objects, so they can be reassigned to +// different variable names and passed to other functions as arguments - for +// example, when supplying an event handler: +function myFunction(){ + // this code will be called in 5 seconds' time +} +setTimeout(myFunction, 5000) + +// You can even write the function statement directly in the call to the other +// function. + +setTimeout(function myFunction(){ + // this code will be called in 5 seconds' time +}, 5000) + +// JavaScript has function scope; functions get their own scope but other blocks +// do not. +if (true){ + var i = 5 +} +i // = 5 - not undefined as you'd expect in a block-scoped language + +// This has led to a common pattern of "immediately-executing anonymous +// functions", which prevent temporary variables from leaking into the global +// 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 + // different name in non-browser environments such as Node.js. + window.permanent = 10 + // Or, as previously mentioned, we can just leave the var keyword off. + permanent2 = 15 +}() +temporary // raises ReferenceError +permanent // = 10 +permanent2 // = 15 + +// One of JavaScript's most powerful features is closures. If a function is +// defined inside another function, the inner function has access to all the +// outer function's variables. +function sayHelloInFiveSeconds(name){ + var prompt = "Hello, " + name + "!" + function inner(){ + alert(prompt) + } + setTimeout(inner, 5000) + // setTimeout is asynchronous, so this function will finish without waiting + // 5 seconds. However, once the 5 seconds is up, inner will still have + // access to the value of prompt. +} +sayHelloInFiveSeconds("Adam") // will open a popup with "Hello, Adam!" in 5s + /*********** * 6. More about Objects; Constructors and Prototypes ***********/ -- cgit v1.2.3 From 8120eb7ff0c8e96ffe72e299ec02ad6426505835 Mon Sep 17 00:00:00 2001 From: Adam Brenecki Date: Thu, 4 Jul 2013 19:49:35 +0930 Subject: Miscellaneous additions --- javascript.html.markdown | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/javascript.html.markdown b/javascript.html.markdown index bdb386f7..9a51e32a 100644 --- a/javascript.html.markdown +++ b/javascript.html.markdown @@ -10,6 +10,10 @@ Java for more complex web applications, but its tight integration with Web pages and built-in support in browsers has caused it to become far more common than Java in web frontends. +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). @@ -33,7 +37,7 @@ doStuff() * 1. Numbers, Strings and Operators ***********/ -// Javascript has one number type, which is a 64-bit IEEE 754 double. +// Javascript has one number type (which is a 64-bit IEEE 754 double). 3 // = 3 1.5 // = 1.5 @@ -50,7 +54,7 @@ doStuff() // is converted to a signed int *up to* 32 bits. 1 << 2 // = 4 -// Enforce precedence with parentheses +// Precedence is enforced with parentheses. (1 + 3) * 2 // = 8 // There are three special not-a-real-number values: @@ -420,6 +424,14 @@ 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](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) + 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) -- cgit v1.2.3