1
1
mirror of https://github.com/chubin/cheat.sh.git synced 2024-11-22 18:17:38 +03:00

updated test results

This commit is contained in:
Igor Chubin 2020-08-05 09:37:11 +02:00
parent c17183cbec
commit 4b701fba3d
12 changed files with 1520 additions and 1196 deletions

View File

@ -1,10 +1,12 @@
1_Inheritance
1line
2_Multiple_Inheritance
:learn
:list
Advanced
Classes
Comments
Control_Flow
Control_Flow_and_Iterables
Functions
Modules
Primitive_Datatypes_and_Operators
@ -13,6 +15,7 @@ doc
func
hello
lambda
list_comprehension
loops
recursion
rosetta/

File diff suppressed because it is too large Load Diff

View File

@ -2,15 +2,15 @@
1ns Main memory reference: Send 2,000 bytes Read 1,000,000 bytes
▗▖ 100ns over commodity network: sequentially from SSD:
▗▖ 62ns 61.712us
▗▖ 44ns 48.981us
L1 cache reference: 1ns ▗ ▗ 
▗▖ 1.0us
▗▖▗▖▗▖▗▖▗▖▗▖▗▖▗▖▗▖▗▖ SSD random read: 16.0us Disk seek: 2.679433ms
Branch mispredict: 3ns  ▗▖▗ ▗▖▗▖ 
▗▖▗▖▗▖▗▖▗▖▗▖▗▖▗▖▗▖▗▖ SSD random read: 16.0us Disk seek: 2.499999ms
Branch mispredict: 3ns  ▗▖▗ ▗▖▗▖ 
▗▖▗▖▗▖ 
Compress 1KB with Snappy: Read 1,000,000 bytes Read 1,000,000 bytes
Compress 1KB wth Snappy: Read 1,000,000 bytes Read 1,000,000 bytes
L2 cache reference: 4ns 2.0us sequentially from memory: sequentially from disk:
▗▖▗▖▗▖▗▖ ▗▖▗▖▗▖▗▖▗▖▗▖▗▖▗▖▗▖▗▖ 3.738us 947.322us
▗▖▗▖▗▖▗▖ ▗▖▗▖▗▖▗▖▗▖▗▖▗▖▗▖▗▖▗▖ 2.967us 824.692us
▗▖▗▖▗▖▗▖▗▖▗▖▗▖▗▖▗▖▗▖ ▗ ▗ 
Mutex lock/unlock: 16ns  
▗▖▗▖▗▖▗▖▗▖▗▖▗▖▗▖▗▖▗▖  Round trip Packet roundtrip

View File

@ -1,33 +1,33 @@
# Microsoft Azure CLI 2.0
# Command-line tools for Azure
# Microsoft Azure CLI 2.0
# Command-line tools for Azure
# Install Azure CLI 2.0 with one curl command.
# Install Azure CLI 2.0 with one curl command.
curl -L https://aka.ms/InstallAzureCli | bash
# create a resource group named "MyResourceGroup" in the westus2 region of Azure
az group create -n MyResourceGroup -l westus2 
# create a resource group named "MyResourceGroup" in the westus2 region of Azure
az group create -n MyResourceGroup -l westus2
# create a Linux VM using the UbuntuTLS image, with two attached storage disks of 10 GB and 20 GB
# create a Linux VM using the UbuntuTLS image, with two attached storage disks of 10 GB and 20 GB
az vm create -n MyLinuxVM -g MyResourceGroup --ssh-key-value $HOME/.ssh/id_rsa.pub --image UbuntuLTS --data-disk-sizes-gb 10 20
# list VMs
# list VMs
az vm list --output table
# list only VMs having distinct state
# list only VMs having distinct state
az vm list -d --query "[?powerState=='VM running']" --output table
# delete VM (with the name MyLinuxVM in the group MyResourceGroup)
# delete VM (with the name MyLinuxVM in the group MyResourceGroup)
az vm delete -g MyResourceGroup -n MyLinuxVM --yes
# Delete all VMs in a resource group
# Delete all VMs in a resource group
az vm delete --ids $(az vm list -g MyResourceGroup --query "[].id" -o tsv)
# Create an Image based on a running VM
# Create an Image based on a running VM
az vm deallocate -g MyResourceGroup -n MyLinuxVM
az vm generalize -g MyResourceGroup -n MyLinuxVM
az image create --resource-group MyResourceGroup --name MyTestImage --source MyLinuxVM
# Running VM based on a VHD
# Running VM based on a VHD
az storage blob upload --account-name "${account_name}" \
 --account-key "${account_key}" --container-name "${container_name}" --type page \
 --file "${file}" --name "${vhd_name}"
@ -36,55 +36,55 @@
 --name myManagedDisk \
 --source https://${account_name}.blob.core.windows.net/${container_name}/${vhd_name}
# open port
# open port
az vm open-port --resource-group MyResourceGroup --name MyLinuxVM --port 443 --priority 899
# Show storage accounts
# Show storage accounts
az storage account list --output table
# Show contaniers for an account
# Show contaniers for an account
az storage container list --account-name mystorageaccount --output table
# Show blobs in a container
# Show blobs in a container
az storage blob list --account-name mystorageaccount --container-name mycontainer --output table
# list account keys
# list account keys
az storage account keys list --account-name STORAGE_NAME --resource-group RESOURCE_GROUP
# Show own images
# Show own images
az image list --output table
# Configure default storage location
# Configure default storage location
az configure --defaults location=eastus2
# Show disks
# Show disks
az disk list --output table
# Copy blob
# Copy blob
az storage blob copy start \
 --source-uri 'https://md-ldh5nknx2rkz.blob.core.windows.net/jzwuuuzzapn0/abcd?sv=2017-04-17&sr=b&si=68041718-6828-4f5e-9e6e-a1b719975062&sig=XXX' \
 --account-key XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX== \
 --account-name destaccount \
 --destination-container vms \
 --destination-blob DESTINATION-blob.vhd
 --source-uri 'https://md-ldh5nknx2rkz.blob.core.windows.net/jzwuuuzzapn0/abcd?sv=2017-04-17&sr=b&si=68041718-6828-4f5e-9e6e-a1b719975062&sig=XXX' \
 --account-key XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX== \
 --account-name destaccount \
 --destination-container vms \
 --destination-blob DESTINATION-blob.vhd
# List virtual networks
# List virtual networks
az network vnet list --output table
# List virtual networks adapters
# List virtual networks adapters
az network nic list --output table
# List public IP addresses used by the VMs
# List public IP addresses used by the VMs
az vm list-ip-addresses --output table
# create snapshot
# create snapshot
az snapshot create --resource-group IC-EXASOL-001 --source vm1-disk1 -n vm1-snap1
# create SAS url for a snapshot
# create SAS url for a snapshot
az snapshot grant-access --resource-group IC-EXASOL-001 --name vm1-snap1 --duration-in-seconds 36000 --query '[accessSas]' -o tsv
# attach disk
# attach disk
az vm disk attach --vm-name vm1 -g RESOURCE_GROUP --disk DISK1_ID
# detach disk
# detach disk
az vm disk detach --vm-name vm1 -g RESOURCE_GROUP --name DISK1_ID

View File

@ -1,19 +1,19 @@
>>> s = 'abcdefgh'
>>> n, m, char, chars = 2, 3, 'd', 'cd'
>>> # starting from n=2 characters in and m=3 in length;
>>> # starting from n=2 characters in and m=3 in length;
>>> s[n-1:n+m-1]
'bcd'
>>> # starting from n characters in, up to the end of the string;
>>> # starting from n characters in, up to the end of the string;
>>> s[n-1:]
'bcdefgh'
>>> # whole string minus last character;
>>> # whole string minus last character;
>>> s[:-1]
'abcdefg'
>>> # starting from a known character char="d" within the string and of m length;
>>> # starting from a known character char="d" within the string and of m length;
>>> indx = s.index(char)
>>> s[indx:indx+m]
'def'
>>> # starting from a known substring chars="cd" within the string and of m length.
>>> # starting from a known substring chars="cd" within the string and of m length.
>>> indx = s.index(chars)
>>> s[indx:indx+m]
'cde'

View File

@ -1,17 +1,17 @@
# Displays everything in the target directory
ls path/to/the/target/directory
# To display everything in <dir>, excluding hidden files:
ls <dir>
# Displays everything including hidden files
ls -a
# To display everything in <dir>, including hidden files:
ls -a <dir>
# Displays all files, along with the size (with unit suffixes) and timestamp
ls -lh 
# To display all files, along with the size (with unit suffixes) and timestamp
ls -lh <dir>
# Display files, sorted by size
ls -S
# To display files, sorted by size:
ls -S <dir>
# Display directories only
ls -d */
# To display directories only:
ls -d */ <dir>
# Display directories only, include hidden
ls -d .*/ */
# To display directories only, include hidden:
ls -d .*/ */ <dir>

View File

@ -65,7 +65,9 @@ Bitmap-Bresenhams-line-algorithm
Bitmap-Flood-fill
Bitmap-Histogram
Bitmap-Midpoint-circle-algorithm
Bitmap-PPM-conversion-through-a-pipe
Bitmap-Read-a-PPM-file
Bitmap-Read-an-image-through-a-pipe
Bitmap-Write-a-PPM-file
Bitwise-IO
Bitwise-operations
@ -92,10 +94,12 @@ Casting-out-nines
Catalan-numbers
Catalan-numbers-Pascals-triangle
Catamorphism
Catmull-Clark-subdivision-surface
Character-codes
Chat-server
Check-Machin-like-formulas
Check-that-file-exists
Checkpoint-synchronization
Chinese-remainder-theorem
Cholesky-decomposition
Circles-of-given-radius-through-two-points
@ -107,6 +111,7 @@ Color-of-a-screen-pixel
Color-quantization
Colour-bars-Display
Colour-pinstripe-Display
Colour-pinstripe-Printer
Combinations
Combinations-and-permutations
Combinations-with-repetitions
@ -141,6 +146,7 @@ Day-of-the-week
Deal-cards-for-FreeCell
Death-Star
Deconvolution-1D
Deconvolution-2D+
Deepcopy
Define-a-primitive-data-type
Delegates
@ -689,6 +695,7 @@ Variadic-function
Vector-products
Verify-distribution-uniformity-Chi-squared-test
Verify-distribution-uniformity-Naive
Video-display-modes
Vigen-re-cipher
Vigen-re-cipher-Cryptanalysis
Visualize-a-tree
@ -715,6 +722,7 @@ Y-combinator
Yahoo--search-interface
Yin-and-yang
Zebra-puzzle
Zeckendorf-arithmetic
Zeckendorf-number-representation
Zero-to-the-zero-power
Zhang-Suen-thinning-algorithm

View File

@ -1,270 +1,270 @@
// Single-line comments start with two slashes.
/* Multiline comments start with slash-star,
 and end with star-slash */
// Single-line comments start with two slashes.
/* Multiline comments start with slash-star,
 and end with star-slash */
// Statements can be terminated by ;
// 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.
// ... but they don't have to be, as semicolons are automatically inserted
// wherever there's a newline, except in certain cases.
doStuff()
// Because those cases can cause unexpected results, we'll keep on using
// semicolons in this guide.
// Because those cases can cause unexpected results, we'll keep on using
// semicolons in this guide.
///////////////////////////////////
// 1. Numbers, Strings and Operators
///////////////////////////////////
// 1. Numbers, Strings and Operators
// 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.
3; // = 3
1.5; // = 1.5
// 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.
3; // = 3
1.5; // = 1.5
// Some basic arithmetic works as you'd expect.
1 + 1; // = 2
0.1 + 0.2; // = 0.30000000000000004
8 - 1; // = 7
10 * 2; // = 20
35 / 5; // = 7
// Some basic arithmetic works as you'd expect.
1 + 1; // = 2
0.1 + 0.2; // = 0.30000000000000004
8 - 1; // = 7
10 * 2; // = 20
35 / 5; // = 7
// Including uneven division.
5 / 2; // = 2.5
// Including uneven division.
5 / 2; // = 2.5
// And modulo division.
10 % 2; // = 0
30 % 4; // = 2
18.5 % 7; // = 4.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
// 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
// Precedence is enforced with parentheses.
(1 + 3) * 2; // = 8
// Precedence is enforced 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, stands for 'Not a Number'
// 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, stands for 'Not a Number'
// There's also a boolean type.
// There's also a boolean type.
true;
false;
// Strings are created with ' or ".
// Strings are created with ' or ".
'abc';
"Hello, world";
// Negation uses the ! symbol
!true; // = false
!false; // = true
// Negation uses the ! symbol
!true; // = false
!false; // = true
// Equality is ===
1 === 1; // = true
2 === 1; // = false
// Equality is ===
1 === 1; // = true
2 === 1; // = false
// Inequality is !==
1 !== 1; // = false
2 !== 1; // = true
// Inequality is !==
1 !== 1; // = false
2 !== 1; // = true
// More comparisons
1 < 10; // = true
1 > 10; // = false
2 <= 2; // = true
2 >= 2; // = true
// More comparisons
1 < 10; // = true
1 > 10; // = false
2 <= 2; // = true
2 >= 2; // = true
// Strings are concatenated with +
"Hello " + "world!"; // = "Hello world!"
// 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,!"
// ... 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
// and are compared with < and >
"a" < "b"; // = true
// Type coercion is performed for comparisons with double equals...
"5" == 5; // = true
null == undefined; // = true
// Type coercion is performed for comparisons with double equals...
"5" == 5; // = true
null == undefined; // = true
// ...unless you use ===
"5" === 5; // = false
null === undefined; // = false
// ...unless you use ===
"5" === 5; // = false
null === undefined; // = false
// ...which can result in some weird behaviour...
13 + !0; // 14
"13" + !0; // '13true'
// ...which can result in some weird behaviour...
13 + !0; // 14
"13" + !0; // '13true'
// You can access characters in a string with `charAt`
"This is a string".charAt(0); // = 'T'
// You can access characters in a string with `charAt`
"This is a string".charAt(0); // = 'T'
// ...or use `substring` to get larger pieces.
"Hello world".substring(0, 5); // = "Hello"
// ...or use `substring` to get larger pieces.
"Hello world".substring(0, 5); // = "Hello"
// `length` is a property, so don't use ().
"Hello".length; // = 5
// `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
undefined; // used to indicate a value is not currently present (although
 // `undefined` is actually a value itself)
// 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)
// 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".
// 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".
///////////////////////////////////
// 2. Variables, Arrays and 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 `=`
// 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...
// 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
// you defined it in.
// ...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
// 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
// separator
// If you want 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
// 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
// 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.
// Arrays are ordered lists of values, of any type.
var myArray = ["Hello", 45, true];
// Their members can be accessed using the square-brackets subscript syntax.
// Array indices start at zero.
myArray[1]; // = 45
// Their members can be accessed using the square-brackets subscript syntax.
// Array indices start at zero.
myArray[1]; // = 45
// Arrays are mutable and of variable length.
// Arrays are mutable and of variable length.
myArray.push("World");
myArray.length; // = 4
myArray.length; // = 4
// Add/Modify at specific index
// 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
// 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 semicolon
// Join all elements of an array with semicolon
var myArray0 = [32,false,"js",12,56,90];
myArray0.join(";") // = "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]
// 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
// "hi","wr" and "ld"; return removed subarray
myArray0.splice(2,4,"hi","wr","ld"); // = ["js",12,56,90]
// myArray0 === [32,false,"hi","wr","ld"]
// Remove 4 elements starting from index 2, and insert there strings
// "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.
// 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"};
// Keys are strings, but quotes aren't required if they're a valid
// JavaScript identifier. Values can be any type.
// 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 also be accessed using the subscript syntax,
myObj["my other key"]; // = 4
// 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.
myObj.myKey; // = "myValue"
// ... 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;
// If you try to access a value that's not yet set, you'll get undefined.
myObj.myFourthKey; // = undefined
// If you try to access a value that's not yet set, you'll get undefined.
myObj.myFourthKey; // = undefined
///////////////////////////////////
// 3. Logic and Control Structures
///////////////////////////////////
// 3. Logic and Control Structures
// 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
 // evaluated if count is 3
} else if (count == 4){
 // evaluated if count is 4
 // evaluated if count is 4
} else {
 // 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){
 // An infinite loop!
 // An infinite loop!
}
// Do-while loops are like while loops, except they always run at least once.
// 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:
// initialization; continue condition; iteration.
// The `for` loop is the same as C and Java:
// initialization; continue condition; iteration.
for (var i = 0; i < 5; i++){
 // will run 5 times
 // will run 5 times
}
// Breaking out of labeled loops is similar to Java
// 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
 // breaks out of outer loop instead of only the inner one
 }
 }
}
// The for/in statement allows iteration over properties of an object.
// 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 '
} // description = 'Paul Ken 18 '
// The for/of statement allows iteration over iterable objects (including the built-in String, 
// Array, e.g. the Array-like arguments or NodeList objects, TypedArray, Map and Set, 
// and user-defined iterables).
// The for/of statement allows iteration over iterable objects (including the built-in String, 
// Array, e.g. the Array-like arguments or NodeList objects, TypedArray, Map and Set, 
// and user-defined iterables).
var myPets = "";
var pets = ["cat", "dog", "hamster", "hedgehog"];
for (var pet of pets){
 myPets += pet + " ";
} // myPets = 'cat dog hamster hedgehog '
} // myPets = 'cat dog hamster hedgehog '
// && is logical and, || is logical or
// && 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
 // colour is either red or blue
}
// && and || "short circuit", which is useful for setting default values.
// && 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.
// 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';
switch (grade) {
 case 'A':
@ -282,172 +282,172 @@
}
///////////////////////////////////
// 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){
 return thing.toUpperCase();
}
myFunction("foo"); // = "FOO"
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.
// 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(){
 return // <- semicolon automatically inserted here
 return // <- semicolon automatically inserted here
 {thisIsAn: 'object literal'};
}
myFunction(); // = undefined
myFunction(); // = undefined
// 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:
// 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
 // this code will be called in 5 seconds' time
}
setTimeout(myFunction, 5000);
// Note: setTimeout isn't part of the JS language, but is provided by browsers
// and Node.js.
// Note: setTimeout isn't part of the JS language, but is provided by browsers
// and Node.js.
// Another function provided by browsers is setInterval
// Another function provided by browsers is setInterval
function myFunction(){
 // this code will be called every 5 seconds
 // 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.
// 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(){
 // this code will be called in 5 seconds' time
 // this code will be called in 5 seconds' time
}, 5000);
// JavaScript has function scope; functions get their own scope but other blocks
// do not.
// 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
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.
// 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 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.
 // 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;
})();
temporary; // raises ReferenceError
permanent; // = 10
temporary; // raises ReferenceError
permanent; // = 10
// 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, even after the outer function exits.
// 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, even after the outer function exits.
function sayHelloInFiveSeconds(name){
 var prompt = "Hello, " + name + "!";
 // Inner functions are put in the local scope by default, as if they were
 // declared with `var`.
 // Inner functions are put in the local scope by default, as if they were
 // declared with `var`.
 function inner(){
 alert(prompt);
 }
 setTimeout(inner, 5000);
 // 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.
 // 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.
}
sayHelloInFiveSeconds("Adam"); // will open a popup with "Hello, Adam!" in 5s
sayHelloInFiveSeconds("Adam"); // will open a popup with "Hello, Adam!" in 5s
///////////////////////////////////
// 5. More about Objects; Constructors and Prototypes
///////////////////////////////////
// 5. More about Objects; Constructors and Prototypes
// Objects can contain functions.
// Objects can contain functions.
var myObj = {
 myFunc: function(){
 return "Hello world!";
 }
};
myObj.myFunc(); // = "Hello world!"
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.
// 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(){
 return this.myString;
 }
};
myObj.myFunc(); // = "Hello world!"
myObj.myFunc(); // = "Hello world!"
// 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.
// 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
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.
// 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.
var myOtherFunc = function(){
 return this.myString.toUpperCase();
};
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
// using `call` or `apply`.
// We can also specify a context for a function to execute in when we invoke it
// using `call` or `apply`.
var anotherFunc = function(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 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
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!"
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
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
// called like that are called constructors.
// 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.
var MyConstructor = function(){
 this.myNumber = 5;
};
myNewObj = new MyConstructor(); // = {myNumber: 5}
myNewObj.myNumber; // = 5
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'.
// 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.
// 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.
// 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!"
};
@ -459,58 +459,58 @@
};
myObj.__proto__ = myPrototype;
myObj.meaningOfLife; // = 42
myObj.meaningOfLife; // = 42
// This works for functions, too.
myObj.myFunc(); // = "hello world!"
// 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.
// 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
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.
// 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.
myPrototype.meaningOfLife = 43;
myObj.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.
// 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!
// 43
// [Function: myFunc]
// true
///prints:
// Hello world!
// 43
// [Function: myFunc]
// true
// To only consider properties attached to the object itself
// and not its prototypes, use the `hasOwnProperty()` check.
// 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!
///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.
// 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.
// The first is Object.create, which is a recent addition to JS, and therefore
// not available in all implementations yet.
// 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
myObj.meaningOfLife; // = 43
// 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.
// 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 = {
 myNumber: 5,
 getMyNumber: function(){
@ -518,47 +518,89 @@
 }
};
var myNewObj2 = new MyConstructor();
myNewObj2.getMyNumber(); // = 5
myNewObj2.getMyNumber(); // = 5
myNewObj2.myNumber = 6;
myNewObj2.getMyNumber(); // = 6
myNewObj2.getMyNumber(); // = 6
// Built-in types like strings and numbers also have constructors that create
// equivalent wrapper objects.
// 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
myNumber == myNumberObj; // = true
// Except, they aren't exactly equivalent.
typeof myNumber; // = 'number'
typeof myNumberObj; // = 'object'
myNumber === myNumberObj; // = false
// 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.
 // 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.
 // 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.
// 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"
"abc".firstCharacter(); // = "a"
// 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.
// 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 with this polyfill:
if (Object.create === undefined){ // don't overwrite it if it exists
// For instance, we mentioned that Object.create isn't yet available in all
// 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
 // make a temporary constructor with the right prototype
 var Constructor = function(){};
 Constructor.prototype = proto;
 // then use it to create a new, appropriately-prototyped object
 // then use it to create a new, appropriately-prototyped object
 return new Constructor();
 };
}
// ES6 Additions
// The "let" keyword allows you to define variables in a lexical scope, 
// as opposed to a block scope like the var keyword does.
let name = "Billy";
// Variables defined with let can be reassigned new values.
name = "William";
// The "const" keyword allows you to define a variable in a lexical scope
// like with let, but you cannot reassign the value once one has been assigned.
const pi = 3.14;
pi = 4.13; // You cannot do this.
// There is a new syntax for functions in ES6 known as "lambda syntax".
// This allows functions to be defined in a lexical scope like with variables
// defined by const and let. 
const isEven = (number) => {
 return number % 2 === 0;
};
isEven(7); // false
// The "equivalent" of this function in the traditional syntax would look like this:
function isEven(number) {
 return number % 2 === 0;
};
// I put the word "equivalent" in double quotes because a function defined
// using the lambda syntax cannnot be called before the definition.
// The following is an example of invalid usage:
add(1, 8);
const add = (firstNumber, secondNumber) => {
 return firstNumber + secondNumber;
};

View File

@ -1,270 +1,270 @@
// Single-line comments start with two slashes.
/* Multiline comments start with slash-star,
 and end with star-slash */
// Single-line comments start with two slashes.
/* Multiline comments start with slash-star,
 and end with star-slash */
// Statements can be terminated by ;
// 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.
// ... but they don't have to be, as semicolons are automatically inserted
// wherever there's a newline, except in certain cases.
doStuff()
// Because those cases can cause unexpected results, we'll keep on using
// semicolons in this guide.
// Because those cases can cause unexpected results, we'll keep on using
// semicolons in this guide.
///////////////////////////////////
// 1. Numbers, Strings and Operators
///////////////////////////////////
// 1. Numbers, Strings and Operators
// 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.
3; // = 3
1.5; // = 1.5
// 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.
3; // = 3
1.5; // = 1.5
// Some basic arithmetic works as you'd expect.
1 + 1; // = 2
0.1 + 0.2; // = 0.30000000000000004
8 - 1; // = 7
10 * 2; // = 20
35 / 5; // = 7
// Some basic arithmetic works as you'd expect.
1 + 1; // = 2
0.1 + 0.2; // = 0.30000000000000004
8 - 1; // = 7
10 * 2; // = 20
35 / 5; // = 7
// Including uneven division.
5 / 2; // = 2.5
// Including uneven division.
5 / 2; // = 2.5
// And modulo division.
10 % 2; // = 0
30 % 4; // = 2
18.5 % 7; // = 4.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
// 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
// Precedence is enforced with parentheses.
(1 + 3) * 2; // = 8
// Precedence is enforced 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, stands for 'Not a Number'
// 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, stands for 'Not a Number'
// There's also a boolean type.
// There's also a boolean type.
true;
false;
// Strings are created with ' or ".
// Strings are created with ' or ".
'abc';
"Hello, world";
// Negation uses the ! symbol
!true; // = false
!false; // = true
// Negation uses the ! symbol
!true; // = false
!false; // = true
// Equality is ===
1 === 1; // = true
2 === 1; // = false
// Equality is ===
1 === 1; // = true
2 === 1; // = false
// Inequality is !==
1 !== 1; // = false
2 !== 1; // = true
// Inequality is !==
1 !== 1; // = false
2 !== 1; // = true
// More comparisons
1 < 10; // = true
1 > 10; // = false
2 <= 2; // = true
2 >= 2; // = true
// More comparisons
1 < 10; // = true
1 > 10; // = false
2 <= 2; // = true
2 >= 2; // = true
// Strings are concatenated with +
"Hello " + "world!"; // = "Hello world!"
// 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,!"
// ... 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
// and are compared with < and >
"a" < "b"; // = true
// Type coercion is performed for comparisons with double equals...
"5" == 5; // = true
null == undefined; // = true
// Type coercion is performed for comparisons with double equals...
"5" == 5; // = true
null == undefined; // = true
// ...unless you use ===
"5" === 5; // = false
null === undefined; // = false
// ...unless you use ===
"5" === 5; // = false
null === undefined; // = false
// ...which can result in some weird behaviour...
13 + !0; // 14
"13" + !0; // '13true'
// ...which can result in some weird behaviour...
13 + !0; // 14
"13" + !0; // '13true'
// You can access characters in a string with `charAt`
"This is a string".charAt(0); // = 'T'
// You can access characters in a string with `charAt`
"This is a string".charAt(0); // = 'T'
// ...or use `substring` to get larger pieces.
"Hello world".substring(0, 5); // = "Hello"
// ...or use `substring` to get larger pieces.
"Hello world".substring(0, 5); // = "Hello"
// `length` is a property, so don't use ().
"Hello".length; // = 5
// `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
undefined; // used to indicate a value is not currently present (although
 // `undefined` is actually a value itself)
// 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)
// 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".
// 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".
///////////////////////////////////
// 2. Variables, Arrays and 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 `=`
// 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...
// 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
// you defined it in.
// ...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
// 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
// separator
// If you want 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
// 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
// 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.
// Arrays are ordered lists of values, of any type.
var myArray = ["Hello", 45, true];
// Their members can be accessed using the square-brackets subscript syntax.
// Array indices start at zero.
myArray[1]; // = 45
// Their members can be accessed using the square-brackets subscript syntax.
// Array indices start at zero.
myArray[1]; // = 45
// Arrays are mutable and of variable length.
// Arrays are mutable and of variable length.
myArray.push("World");
myArray.length; // = 4
myArray.length; // = 4
// Add/Modify at specific index
// 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
// 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 semicolon
// Join all elements of an array with semicolon
var myArray0 = [32,false,"js",12,56,90];
myArray0.join(";") // = "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]
// 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
// "hi","wr" and "ld"; return removed subarray
myArray0.splice(2,4,"hi","wr","ld"); // = ["js",12,56,90]
// myArray0 === [32,false,"hi","wr","ld"]
// Remove 4 elements starting from index 2, and insert there strings
// "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.
// 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"};
// Keys are strings, but quotes aren't required if they're a valid
// JavaScript identifier. Values can be any type.
// 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 also be accessed using the subscript syntax,
myObj["my other key"]; // = 4
// 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.
myObj.myKey; // = "myValue"
// ... 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;
// If you try to access a value that's not yet set, you'll get undefined.
myObj.myFourthKey; // = undefined
// If you try to access a value that's not yet set, you'll get undefined.
myObj.myFourthKey; // = undefined
///////////////////////////////////
// 3. Logic and Control Structures
///////////////////////////////////
// 3. Logic and Control Structures
// 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
 // evaluated if count is 3
} else if (count == 4){
 // evaluated if count is 4
 // evaluated if count is 4
} else {
 // 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){
 // An infinite loop!
 // An infinite loop!
}
// Do-while loops are like while loops, except they always run at least once.
// 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:
// initialization; continue condition; iteration.
// The `for` loop is the same as C and Java:
// initialization; continue condition; iteration.
for (var i = 0; i < 5; i++){
 // will run 5 times
 // will run 5 times
}
// Breaking out of labeled loops is similar to Java
// 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
 // breaks out of outer loop instead of only the inner one
 }
 }
}
// The for/in statement allows iteration over properties of an object.
// 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 '
} // description = 'Paul Ken 18 '
// The for/of statement allows iteration over iterable objects (including the built-in String, 
// Array, e.g. the Array-like arguments or NodeList objects, TypedArray, Map and Set, 
// and user-defined iterables).
// The for/of statement allows iteration over iterable objects (including the built-in String, 
// Array, e.g. the Array-like arguments or NodeList objects, TypedArray, Map and Set, 
// and user-defined iterables).
var myPets = "";
var pets = ["cat", "dog", "hamster", "hedgehog"];
for (var pet of pets){
 myPets += pet + " ";
} // myPets = 'cat dog hamster hedgehog '
} // myPets = 'cat dog hamster hedgehog '
// && is logical and, || is logical or
// && 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
 // colour is either red or blue
}
// && and || "short circuit", which is useful for setting default values.
// && 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.
// 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';
switch (grade) {
 case 'A':
@ -282,172 +282,172 @@
}
///////////////////////////////////
// 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){
 return thing.toUpperCase();
}
myFunction("foo"); // = "FOO"
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.
// 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(){
 return // <- semicolon automatically inserted here
 return // <- semicolon automatically inserted here
 {thisIsAn: 'object literal'};
}
myFunction(); // = undefined
myFunction(); // = undefined
// 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:
// 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
 // this code will be called in 5 seconds' time
}
setTimeout(myFunction, 5000);
// Note: setTimeout isn't part of the JS language, but is provided by browsers
// and Node.js.
// Note: setTimeout isn't part of the JS language, but is provided by browsers
// and Node.js.
// Another function provided by browsers is setInterval
// Another function provided by browsers is setInterval
function myFunction(){
 // this code will be called every 5 seconds
 // 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.
// 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(){
 // this code will be called in 5 seconds' time
 // this code will be called in 5 seconds' time
}, 5000);
// JavaScript has function scope; functions get their own scope but other blocks
// do not.
// 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
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.
// 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 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.
 // 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;
})();
temporary; // raises ReferenceError
permanent; // = 10
temporary; // raises ReferenceError
permanent; // = 10
// 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, even after the outer function exits.
// 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, even after the outer function exits.
function sayHelloInFiveSeconds(name){
 var prompt = "Hello, " + name + "!";
 // Inner functions are put in the local scope by default, as if they were
 // declared with `var`.
 // Inner functions are put in the local scope by default, as if they were
 // declared with `var`.
 function inner(){
 alert(prompt);
 }
 setTimeout(inner, 5000);
 // 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.
 // 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.
}
sayHelloInFiveSeconds("Adam"); // will open a popup with "Hello, Adam!" in 5s
sayHelloInFiveSeconds("Adam"); // will open a popup with "Hello, Adam!" in 5s
///////////////////////////////////
// 5. More about Objects; Constructors and Prototypes
///////////////////////////////////
// 5. More about Objects; Constructors and Prototypes
// Objects can contain functions.
// Objects can contain functions.
var myObj = {
 myFunc: function(){
 return "Hello world!";
 }
};
myObj.myFunc(); // = "Hello world!"
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.
// 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(){
 return this.myString;
 }
};
myObj.myFunc(); // = "Hello world!"
myObj.myFunc(); // = "Hello world!"
// 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.
// 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
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.
// 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.
var myOtherFunc = function(){
 return this.myString.toUpperCase();
};
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
// using `call` or `apply`.
// We can also specify a context for a function to execute in when we invoke it
// using `call` or `apply`.
var anotherFunc = function(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 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
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!"
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
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
// called like that are called constructors.
// 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.
var MyConstructor = function(){
 this.myNumber = 5;
};
myNewObj = new MyConstructor(); // = {myNumber: 5}
myNewObj.myNumber; // = 5
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'.
// 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.
// 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.
// 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!"
};
@ -459,58 +459,58 @@
};
myObj.__proto__ = myPrototype;
myObj.meaningOfLife; // = 42
myObj.meaningOfLife; // = 42
// This works for functions, too.
myObj.myFunc(); // = "hello world!"
// 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.
// 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
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.
// 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.
myPrototype.meaningOfLife = 43;
myObj.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.
// 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!
// 43
// [Function: myFunc]
// true
///prints:
// Hello world!
// 43
// [Function: myFunc]
// true
// To only consider properties attached to the object itself
// and not its prototypes, use the `hasOwnProperty()` check.
// 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!
///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.
// 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.
// The first is Object.create, which is a recent addition to JS, and therefore
// not available in all implementations yet.
// 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
myObj.meaningOfLife; // = 43
// 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.
// 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 = {
 myNumber: 5,
 getMyNumber: function(){
@ -518,47 +518,89 @@
 }
};
var myNewObj2 = new MyConstructor();
myNewObj2.getMyNumber(); // = 5
myNewObj2.getMyNumber(); // = 5
myNewObj2.myNumber = 6;
myNewObj2.getMyNumber(); // = 6
myNewObj2.getMyNumber(); // = 6
// Built-in types like strings and numbers also have constructors that create
// equivalent wrapper objects.
// 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
myNumber == myNumberObj; // = true
// Except, they aren't exactly equivalent.
typeof myNumber; // = 'number'
typeof myNumberObj; // = 'object'
myNumber === myNumberObj; // = false
// 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.
 // 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.
 // 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.
// 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"
"abc".firstCharacter(); // = "a"
// 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.
// 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 with this polyfill:
if (Object.create === undefined){ // don't overwrite it if it exists
// For instance, we mentioned that Object.create isn't yet available in all
// 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
 // make a temporary constructor with the right prototype
 var Constructor = function(){};
 Constructor.prototype = proto;
 // then use it to create a new, appropriately-prototyped object
 // then use it to create a new, appropriately-prototyped object
 return new Constructor();
 };
}
// ES6 Additions
// The "let" keyword allows you to define variables in a lexical scope, 
// as opposed to a block scope like the var keyword does.
let name = "Billy";
// Variables defined with let can be reassigned new values.
name = "William";
// The "const" keyword allows you to define a variable in a lexical scope
// like with let, but you cannot reassign the value once one has been assigned.
const pi = 3.14;
pi = 4.13; // You cannot do this.
// There is a new syntax for functions in ES6 known as "lambda syntax".
// This allows functions to be defined in a lexical scope like with variables
// defined by const and let. 
const isEven = (number) => {
 return number % 2 === 0;
};
isEven(7); // false
// The "equivalent" of this function in the traditional syntax would look like this:
function isEven(number) {
 return number % 2 === 0;
};
// I put the word "equivalent" in double quotes because a function defined
// using the lambda syntax cannnot be called before the definition.
// The following is an example of invalid usage:
add(1, 8);
const add = (firstNumber, secondNumber) => {
 return firstNumber + secondNumber;
};

View File

@ -1,17 +1,17 @@
# Displays everything in the target directory
ls path/to/the/target/directory
# To display everything in <dir>, excluding hidden files:
ls <dir>
# Displays everything including hidden files
ls -a
# To display everything in <dir>, including hidden files:
ls -a <dir>
# Displays all files, along with the size (with unit suffixes) and timestamp
ls -lh
# To display all files, along with the size (with unit suffixes) and timestamp
ls -lh <dir>
# Display files, sorted by size
ls -S
# To display files, sorted by size:
ls -S <dir>
# Display directories only
ls -d */
# To display directories only:
ls -d */ <dir>
# Display directories only, include hidden
ls -d .*/ */
# To display directories only, include hidden:
ls -d .*/ */ <dir>

View File

@ -1,54 +1,54 @@
# Create a btrfs file system on /dev/sdb, /dev/sdc, and /dev/sdd
# Create a btrfs file system on /dev/sdb, /dev/sdc, and /dev/sdd
mkfs.btrfs /dev/sdb /dev/sdc /dev/sdd
# btrfs with just one hard drive, metadata not redundant 
# (this is danegerous: if your metadata is lost, your data is lost as well)
# btrfs with just one hard drive, metadata not redundant
# (this is danegerous: if your metadata is lost, your data is lost as well)
mkfs.btrfs -m single /dev/sdb
# data to be redundant and metadata to be non-redundant:
# data to be redundant and metadata to be non-redundant:
mkfs.btrfs -m raid0 -d raid1 /dev/sdb /dev/sdc /dev/sdd
# both data and metadata to be redundan
# both data and metadata to be redundan
mkfs.btrfs -d raid1 /dev/sdb /dev/sdc /dev/sdd
# To get a list of all btrfs file systems
# To get a list of all btrfs file systems
btrfs filesystem show
# detailed df for a fileesystem (mounted in /mnt)
# detailed df for a fileesystem (mounted in /mnt)
btrfs filesystem df /mnt
# resize btrfs online (-2g decreases, +2g increases)
# resize btrfs online (-2g decreases, +2g increases)
btrfs filesystem resize -2g /mnt
# use maximum space
# use maximum space
btrfs filesystem resize max /mnt
# add new device to a filesystem
# add new device to a filesystem
btrfs device add /dev/sdf /mnt
# remove devices from a filesystem
# remove devices from a filesystem
btrfs device delete missing /mnt
# create the subvolume /mnt/sv1 in the /mnt volume
# create the subvolume /mnt/sv1 in the /mnt volume
btrfs subvolume create /mnt/sv1
# list subvolumes
# list subvolumes
btrfs subvolume list /mnt
# mount subvolume without mounting the main filesystem
# mount subvolume without mounting the main filesystem
mount -o subvol=sv1 /dev/sdb /mnt
# delete subvolume
# delete subvolume
btrfs subvolume delete /mnt/sv1
# taking snapshot of a subvolume
# taking snapshot of a subvolume
btrfs subvolume snapshot /mnt/sv1 /mnt/sv1_snapshot
# taking snapshot of a file (copy file by reference)
# taking snapshot of a file (copy file by reference)
cp --reflink /mnt/sv1/test1 /mnt/sv1/test3
# convert ext3/ext4 to btrfs
# convert ext3/ext4 to btrfs
btrfs-convert /dev/sdb1
# convert btrfs to ext3/ext4
# convert btrfs to ext3/ext4
btrfs-convert -r /dev/sdb1

View File

@ -1,3 +1,3 @@
 btrfs 
# create the subvolume /mnt/sv1 in the /mnt volume
# create the subvolume /mnt/sv1 in the /mnt volume
btrfs subvolume create /mnt/sv1