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)

0 commit comments

Comments
 (0)