Skip to content

Commit d695c12

Browse files
committed
Initial commit
0 parents  commit d695c12

File tree

238 files changed

+8037
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

238 files changed

+8037
-0
lines changed

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Auto detect text files and perform LF normalization
2+
* text=auto

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2020 w3bdesign
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Modern JavaScript for the Impatient
2+
Code for the book Modern JavaScript for the Impatient

ch1/sec1/first.html

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<html>
2+
<head>
3+
<title>My First JavaScript Program</title>
4+
<script type="text/javascript">
5+
let a = 6
6+
let b = 7
7+
window.alert(a * b)
8+
</script>
9+
</head>
10+
<body>
11+
</body>
12+
</html>

ch1/sec1/first.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// node first.js
2+
3+
let a = 6
4+
let b = 7
5+
console.log(a * b)

ch1/sec10/strings.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// node strings.js
2+
3+
'use strict'
4+
// Escape sequences
5+
let s = '\\\'\'\\\n'
6+
console.log('s:', s) // \''\
7+
console.log('s.length:', s.length) // 5
8+
// Strings can hold arbitrary Unicode characters
9+
let greeting = 'Hello 🌐'
10+
console.log('greeting:', greeting) // Hello 🌐
11+
// Characters above \u{FFFF} require two code units
12+
greeting = 'Hello \u{1F310}'
13+
console.log('greeting:', greeting) // Hello 🌐
14+
console.log('greeting.length:', greeting.length) // 8
15+
console.log('greeting[0]:', greeting[0]) // H
16+
console.log('greeting[6]:', greeting[6]) // �
17+
console.log('greeting[7]:', greeting[7]) // �

ch1/sec11/template-literals.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// node template-literals.js
2+
3+
'use strict'
4+
// The values of embedded expressions are spliced in
5+
let destination = 'world' // A regular string
6+
let greeting = `Hello, ${destination.toUpperCase()}!` // A template literal
7+
console.log('greeting:', greeting) // Hello, WORLD!
8+
// Template literals can be nested inside embedded expressions
9+
let firstname = 'Harry'
10+
let lastname = 'Burns'
11+
greeting = `Hello, ${firstname.length > 0 ? `${firstname[0]}.` : '' } ${lastname}`
12+
console.log('greeting:', greeting) // Hello, H. Burns
13+
// Template literals can contain newlines
14+
destination = 'Bermuda'
15+
greeting = `<div>Hello</div>
16+
<div>${destination}</div>
17+
`
18+
console.log('greeting:', greeting) // \''\ followed by a newline
19+
// This function processes a tagged template literal
20+
const html = (fragments, ...values) => { // See section 6.4
21+
const escapeHTML = str => [...str].map(c => c === '<' ? '&lt;'
22+
: c === '&' ? '&amp;' : c).join('')
23+
let result = fragments[0]
24+
for (let i = 0; i < values.length; i++) {
25+
result += escapeHTML(values[i])
26+
result += fragments[i + 1]
27+
}
28+
return result
29+
}
30+
// The tagged template literal is prefixed with the processing function
31+
destination = 'St. Kitts & Nevis'
32+
let message = html`<div>Hello, ${destination}</div>`
33+
console.log('message:', message) // <div>Hello, St. Kitts &amp; Nevis</div>

ch1/sec12/objects.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// node objects.js
2+
3+
'use strict'
4+
console.log('// An object literal')
5+
const harry = { name: 'Harry Smith', age: 42 }
6+
console.log('harry:', harry) // { name: 'Harry Smith', age: 42 }
7+
console.log('// Access properties with the dot notation')
8+
let harrysAge = harry.age
9+
console.log('harrysAge:', harrysAge) // 42
10+
console.log('// You can modify existing properties and add new properties')
11+
harry.age = 40
12+
harry.salary = 90000
13+
console.log('harry:', harry) // { name: 'Harry Smith', age: 40, salary: 90000 }
14+
console.log('// You can mutate the properties of a const')
15+
const sally = { name: 'Sally Lee' }
16+
sally.age = 28 // OK—mutates the object to which sally refers
17+
18+
try {
19+
sally = { name: 'Sally Albright' }
20+
// Error—cannot assign a different value to a const variable
21+
} catch (exception) {
22+
console.log('Error:', exception.message) // Assignment to constant variable.
23+
}
24+
console.log('// The delete operator deletes a property')
25+
delete harry.salary
26+
console.log('harry:', harry) // { name: 'Harry Smith', age: 40 }
27+
console.log('// Accessing a nonexistent property yields undefined')
28+
let boss = harry.supervisor // undefined
29+
console.log('boss:', boss)
30+
console.log('// Use array brackets for computed properties')
31+
let field = 'Age'
32+
harrysAge = harry[field.toLowerCase()]
33+
console.log('harrysAge:', harrysAge) // 40

ch1/sec13/object-literals.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// node object-literals.js
2+
3+
'use strict'
4+
console.log('// An object literal can have trailing commas')
5+
let harry = {
6+
name: 'Harry Smith',
7+
age: 42, // Add more properties below
8+
}
9+
console.log('harry:', harry) // { name: 'Harry Smith', age: 42 }
10+
console.log('// It is common to assign a variable to a property of the same name')
11+
let age = 43
12+
harry = { name: 'Harry Smith', age: age }
13+
// The 'age' property is set to the value of the age variable
14+
console.log('harry:', harry) // { name: 'Harry Smith', age: 43 }
15+
console.log('// Then you can use this shortcut')
16+
harry = { name: 'Harry Smith', age } // The age property is now 43
17+
console.log('harry:', harry) // { name: 'Harry Smith', age: 43 }
18+
console.log('// Use brackets for computed properties')
19+
let field = 'Age'
20+
harry = { name: 'Harry Smith', [field.toLowerCase()] : 42 }
21+
console.log('harry:', harry) // { name: 'Harry Smith', age: 42 }
22+
console.log('// If a property name isn’t an identifier, quote it')
23+
harry = { name: 'Harry Smith', 'favorite beer': 'IPA' }
24+
console.log('harry:', harry) // { name: 'Harry Smith', 'favorite beer': 'IPA' }
25+
console.log('// To access such a property, use brackets')
26+
harry['favorite beer'] = 'Lager'
27+
console.log('harry:', harry) // { name: 'Harry Smith', 'favorite beer': 'Lager' }

ch1/sec14/arrays.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// node arrays.js
2+
3+
'use strict'
4+
console.log('// Array literals are enclosed in square brackets')
5+
const numbers = [1, 2, 3, 'many']
6+
console.log('numbers:', numbers) // [1, 2, 3, 'many']
7+
console.log('// An array can have missing elements')
8+
const someNumbers = [ , 2, , 9] // No properties '0', '2'
9+
console.log('someNumbers:', someNumbers) // [<1 empty item>, 2, <1 empty item>, 9]
10+
console.log('// You can add new elements past the end')
11+
someNumbers[6] = 11 // Now someNumbers has length 7
12+
console.log('someNumbers:', someNumbers) // [<1 empty item>, 2, <1 empty item>, 9, <2 empty items>, 11]
13+
console.log('// A trailing comma does not indicate a missing element')
14+
const developers = [
15+
'Harry Smith',
16+
'Sally Lee',
17+
// Add more elements above
18+
]
19+
console.log('developers:', developers) // ['Harry Smith', 'Sally Lee']
20+
console.log('// Since arrays are objects, you can add arbitrary properties')
21+
numbers.lucky = true
22+
console.log('numbers:', numbers) // [1, 2, 3, 'many', lucky: true]
23+
console.log('// Converting an array to a string')
24+
const str = '' + [1, 2, 3]
25+
console.log('str:', str) // 1,2,3
26+
console.log('// A two-dimensional array is an array of arrays')
27+
const melancholyMagicSquare = [
28+
[16, 3, 2, 13],
29+
[5, 10, 11, 8],
30+
[9, 6, 7, 12],
31+
[4, 15, 14, 1]
32+
]
33+
console.log('melancholyMagicSquare:', melancholyMagicSquare) // [[16, 3, 2, 13], [5, 10, 11, 8], [9, 6, 7, 12], [4, 15, 14, 1]]
34+
console.log('// Use two brackets to access an element')
35+
const element = melancholyMagicSquare[1][2] // 11
36+
console.log('element:', element)

ch1/sec15/json.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// node json.js
2+
3+
'use strict'
4+
console.log('// Parsing a JSON string')
5+
let harry = JSON.parse(
6+
'{ "name": "Harry Smith", "age": 42, "lucky numbers": [17, 29], "lucky": false }'
7+
)
8+
console.log('harry:', harry) // { name: 'Harry Smith', age: 42, 'lucky numbers': [17, 29], lucky: false }
9+
console.log('// No undefined in JSON')
10+
// Undefined properties are dropped
11+
// Undefined array elements turn into null
12+
let str = JSON.stringify({ name: ['Harry', undefined, 'Smith'], age: undefined })
13+
console.log('str:', str) // {"name":["Harry",null,"Smith"]}
14+
console.log('// Useless message:')
15+
console.log(`harry=${harry}`)
16+
17+
console.log('// Remedy with JSON:')
18+
console.log(`harry=${JSON.stringify(harry)}`)
19+
20+
console.log('// Alternative:')
21+
const sally = { name: 'Sally Lee' }
22+
console.log('harry=', harry, 'sally=', sally)
23+
24+
console.log('// Or even easier:')
25+
console.log({harry, sally}) // Logs the object { harry: {

ch1/sec16/destructuring.js

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
// node destructuring.js
2+
3+
'use strict'
4+
// The blocks { . . . } isolate the variable declarations
5+
console.log('// Without destructuring:')
6+
{
7+
let pair = [1, 2]
8+
let first = pair[0]
9+
let second = pair[1]
10+
console.log('first:', first) // 1
11+
console.log('second:', second) // 2
12+
}
13+
console.log('// With destructuring:')
14+
{
15+
let pair = [1, 2]
16+
let [first, second] = pair
17+
console.log('first:', first) // 1
18+
console.log('second:', second) // 2
19+
}
20+
console.log('// Observe the matching:')
21+
{
22+
let [first, [second, third]] = [1, [2, 3]]
23+
// Sets first to 1, second to 2, and third to 3
24+
console.log('first:', first)
25+
console.log('second:', second)
26+
console.log('third:', third)
27+
}
28+
console.log('// Unmatched elements are ignored')
29+
{
30+
let [first, second] = [1, 2, 3]
31+
console.log('first:', first)
32+
console.log('second:', second)
33+
}
34+
console.log('// Unmatched variables are set to undefined')
35+
{
36+
let [first, second] = [1]
37+
// Sets first to 1, second to undefined
38+
console.log('first:', first)
39+
console.log('second:', second)
40+
}
41+
console.log('// Destructuring with already declared variables')
42+
{
43+
let first = 1
44+
let second = 2;
45+
// Caution: Here you need a semicolon--see section 2.2.
46+
[first, second] = [4, 5]
47+
console.log('first:', first) // 4
48+
console.log('second:', second) // 5
49+
}
50+
console.log('// Swapping two variables')
51+
{
52+
let x = 6
53+
let y = 7;
54+
// Caution: Here you need a semicolon--see section 2.2.
55+
[x, y] = [y, x]
56+
console.log('x:', x) // 7
57+
console.log('y:', y) // 6
58+
}
59+
console.log('// Any lvalues can be on the left hand side')
60+
{
61+
const numbers = []
62+
const harry = { name: 'Harry Burns' };
63+
[numbers[0], harry.age] = [13, 42] // Same as numbers[0] = 13; harry.age = 42
64+
console.log('numbers:', numbers) // [13]
65+
console.log('harry:', harry) // { name: 'Harry Burns', age: 42 }
66+
}
67+
console.log('// Use property names when destructuring an object')
68+
{
69+
let harry = { name: 'Harry', age: 42 }
70+
let { name: harrysName, age: harrysAge } = harry
71+
console.log('harrysName:', harrysName) // Harry
72+
console.log('harrysAge:', harrysAge)
73+
}
74+
console.log('// Compelling case; property and variable names are the same')
75+
{
76+
let harry = { name: 'Harry', age: 42 }
77+
let { name, age } = harry
78+
console.log('name:', name) // Harry
79+
console.log('age:', age)
80+
}
81+
console.log('// Caution: Use parentheses when setting existing variables')
82+
{
83+
let name = undefined
84+
let age = undefined
85+
const sally = { name: 'Sally Albright', age: 28 };
86+
// Semicolon AND parentheses required here
87+
({name, age} = sally)
88+
console.log('name:', name) // Sally Albright
89+
console.log('age:', age) // 28
90+
}

ch1/sec17/advanced-destructuring.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// node advanced-destructuring.js
2+
3+
'use strict'
4+
console.log('// Can destructure nested objects')
5+
let pat = { name: 'Pat', birthday: { day: 14, month: 3, year: 2000 } }
6+
let { birthday: { year: patsBirthYear } } = pat
7+
// Declares the variable patsBirthYear and initializes it to 2000
8+
console.log('patsBirthYear:', patsBirthYear)
9+
console.log('// Computed property names are supported')
10+
const harry = { name: 'Harry Burns', age: 42 };
11+
let field = 'Age'
12+
let { [field.toLowerCase()]: harrysAge } = harry
13+
// Sets harrysAge to harry[field.toLowerCase()]
14+
console.log('harrysAge:', harrysAge) // 42
15+
console.log('// Rest declaration captures remaining array elements')
16+
{
17+
const numbers = [1, 7, 2, 9]
18+
let [first, second, ...others] = numbers
19+
// first is 1, second is 7, and others is [2, 9]
20+
console.log('first:', first)
21+
console.log('second:', second)
22+
console.log('others:', others)
23+
}
24+
console.log('// If there are insufficient elements, the rest is an empty array')
25+
{
26+
let [first, second, ...others] = [42]
27+
// first is 42, second is undefined, and others is []
28+
console.log('first:', first)
29+
console.log('second:', second)
30+
console.log('others:', others)
31+
}
32+
console.log('// Rest declarations also work for objects')
33+
{
34+
let { name, ...allButName } = harry
35+
// allButName is { age: 42 }
36+
console.log('name:', name) // Harry Burns
37+
console.log('allButName:', allButName)
38+
}
39+
console.log('// Default is used if desired value is absent')
40+
{
41+
let [first, second = 0] = [42]
42+
// Sets first to 42, second to 0 since the right-hand side has
43+
// no matching element
44+
let { nickname = 'None' } = harry
45+
// Sets nickname to 'None' since harry has no nickname property
46+
console.log('first:', first) // 42
47+
console.log('second:', second) // 0
48+
}
49+
console.log('// Default expressions can use previously set variables')
50+
{
51+
let { name, nickname = name } = harry
52+
// Both name and nickname are set to harry.name
53+
console.log('name:', name) // Harry Burns
54+
console.log('nickname:', nickname) // Harry Burns
55+
}
56+
console.log('// Destructuring with configuration objects')
57+
{
58+
let config = { separator: '; ' }
59+
const { separator = ',', leftDelimiter = '[', rightDelimiter = ']' } = config
60+
console.log('separator:', separator) // ;
61+
console.log('leftDelimiter:', leftDelimiter) // [
62+
console.log('rightDelimiter:', rightDelimiter) // ]
63+
}

0 commit comments

Comments
 (0)