Merge pull request #954 from uvtc/patch-1

javascript/en: added some md formatting
This commit is contained in:
Adam Bard 2015-02-04 03:32:12 +00:00
commit 885228de12

View File

@ -110,19 +110,19 @@ null === undefined; // = false
13 + !0; // 14 13 + !0; // 14
"13" + !0; // '13true' "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' "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" "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 "Hello".length; // = 5
// There's also null and undefined // There's also `null` and `undefined`.
null; // used to indicate a deliberate non-value null; // used to indicate a deliberate non-value
undefined; // used to indicate a value is not currently present (although 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. // 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". // 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 // 2. Variables, Arrays and Objects
// Variables are declared with the var keyword. JavaScript is dynamically typed, // Variables are declared with the `var` keyword. JavaScript is dynamically
// so you don't need to specify type. Assignment uses a single = character. // typed, so you don't need to specify type. Assignment uses a single `=`
// character.
var someVar = 5; 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...
@ -165,7 +166,7 @@ myArray.length; // = 4
// Add/Modify at specific index // Add/Modify at specific index
myArray[3] = "Hello"; 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. // languages: an unordered collection of key-value pairs.
var myObj = {key1: "Hello", key2: "World"}; 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 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; var count = 1;
if (count == 3){ if (count == 3){
// evaluated if count is 3 // evaluated if count is 3
@ -200,7 +201,7 @@ if (count == 3){
// evaluated if it's not either 3 or 4 // evaluated if it's not either 3 or 4
} }
// As does while. // As does `while`.
while (true){ while (true){
// An infinite loop! // An infinite loop!
} }
@ -211,7 +212,7 @@ do {
input = getInput(); input = getInput();
} while (!isValid(input)) } 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. // initialisation; continue condition; iteration.
for (var i = 0; i < 5; i++){ for (var i = 0; i < 5; i++){
// will run 5 times // will run 5 times
@ -229,7 +230,7 @@ if (colour == "red" || colour == "blue"){
var name = otherName || "default"; var name = otherName || "default";
// switch statement checks for equality with === // 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. // or the cases after the correct one will be executed too.
grade = 'B'; grade = 'B';
@ -252,14 +253,14 @@ switch (grade) {
/////////////////////////////////// ///////////////////////////////////
// 4. Functions, Scope and Closures // 4. Functions, Scope and Closures
// JavaScript functions are declared with the function keyword. // JavaScript functions are declared with the `function` keyword.
function myFunction(thing){ function myFunction(thing){
return thing.toUpperCase(); return thing.toUpperCase();
} }
myFunction("foo"); // = "FOO" myFunction("foo"); // = "FOO"
// Note that the value to be returned must start on the same line as the // 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. // automatic semicolon insertion. Watch out for this when using Allman style.
function myFunction() function myFunction()
{ {
@ -298,8 +299,8 @@ i; // = 5 - not undefined as you'd expect in a block-scoped language
// scope. // scope.
(function(){ (function(){
var temporary = 5; var temporary = 5;
// We can access the global scope by assiging to the 'global object', which // 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 // in a web browser is always `window`. The global object may have a
// different name in non-browser environments such as Node.js. // different name in non-browser environments such as Node.js.
window.permanent = 10; window.permanent = 10;
})(); })();
@ -312,7 +313,7 @@ permanent; // = 10
function sayHelloInFiveSeconds(name){ function sayHelloInFiveSeconds(name){
var prompt = "Hello, " + name + "!"; var prompt = "Hello, " + name + "!";
// Inner functions are put in the local scope by default, as if they were // Inner functions are put in the local scope by default, as if they were
// declared with 'var'. // declared with `var`.
function inner(){ function inner(){
alert(prompt); alert(prompt);
} }
@ -320,7 +321,7 @@ function sayHelloInFiveSeconds(name){
// setTimeout is asynchronous, so the sayHelloInFiveSeconds function will // setTimeout is asynchronous, so the sayHelloInFiveSeconds function will
// exit immediately, and setTimeout will call inner afterwards. However, // exit immediately, and setTimeout will call inner afterwards. However,
// because inner is "closed over" sayHelloInFiveSeconds, inner still has // 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 sayHelloInFiveSeconds("Adam"); // will open a popup with "Hello, Adam!" in 5s
@ -336,7 +337,7 @@ var myObj = {
myObj.myFunc(); // = "Hello world!" myObj.myFunc(); // = "Hello world!"
// When functions attached to an object are called, they can access the object // 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 = { myObj = {
myString: "Hello world!", myString: "Hello world!",
myFunc: function(){ myFunc: function(){
@ -352,7 +353,7 @@ var myFunc = myObj.myFunc;
myFunc(); // = undefined myFunc(); // = undefined
// Inversely, a function can be assigned to the object and gain access to it // 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(){ var myOtherFunc = function(){
return this.myString.toUpperCase(); return this.myString.toUpperCase();
} }
@ -360,37 +361,38 @@ myObj.myOtherFunc = myOtherFunc;
myObj.myOtherFunc(); // = "HELLO WORLD!" myObj.myOtherFunc(); // = "HELLO WORLD!"
// We can also specify a context for a function to execute in when we invoke it // 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){ var anotherFunc = function(s){
return this.myString + s; return this.myString + s;
} }
anotherFunc.call(myObj, " And Hello Moon!"); // = "Hello World! And Hello Moon!" 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!" 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 // This is useful when working with a function that accepts a sequence of
// and you want to pass an array. // arguments and you want to pass an array.
Math.min(42, 6, 27); // = 6 Math.min(42, 6, 27); // = 6
Math.min([42, 6, 27]); // = NaN (uh-oh!) Math.min([42, 6, 27]); // = NaN (uh-oh!)
Math.min.apply(Math, [42, 6, 27]); // = 6 Math.min.apply(Math, [42, 6, 27]); // = 6
// But, 'call' and 'apply' are only temporary. When we want it to stick, we can use // But, `call` and `apply` are only temporary. When we want it to stick, we can
// bind. // use `bind`.
var boundFunc = anotherFunc.bind(myObj); var boundFunc = anotherFunc.bind(myObj);
boundFunc(" And Hello Saturn!"); // = "Hello World! And Hello Saturn!" 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 product = function(a, b){ return a * b; }
var doubler = product.bind(this, 2); var doubler = product.bind(this, 2);
doubler(8); // = 16 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 // made available to the function via the this keyword. Functions designed to be
// called like that are called constructors. // called like that are called constructors.