Skip to content

Commit 238e8d4

Browse files
committed
Improve course
1 parent 9b1d73c commit 238e8d4

File tree

2 files changed

+70
-7
lines changed

2 files changed

+70
-7
lines changed

training/front-end/01-modern-javascript.md

Lines changed: 70 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
- [Other features](#other-features)
4141
- [Optional chaining](#optional-chaining)
4242
- [Self assessment](#self-assessment)
43-
- [References](#references)
4443

4544
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
4645

@@ -64,8 +63,8 @@ This course assumes you already have experience with JavaScript. If you don't, s
6463

6564
### Check your knowledge of JS
6665

67-
- What are the 5 JS datatypes?
68-
- `Number`, `String`, `Boolean`, `Object`, `Undefined`
66+
- What are the main JS datatypes?
67+
- `Number`, `String`, `Boolean`, `Object`, `Undefined`, `BigInt`
6968
- What does `1 / "a"` evaluate to?
7069
- `NaN`
7170

@@ -106,6 +105,25 @@ Use [caniuse](https://caniuse.com/) to check what browser support the feature yo
106105

107106
You can then use "polyfills" (or "shims") to support older browser. There are some polyfills for each specific feature, and some other that includes lots of polyfills (e.g. [zloirock/core-js](https://github.com/zloirock/core-js#ecmascript-array)).
108107

108+
Polyfills are different from transpiling. Babel is a transpiler, you
109+
can see how it work online here:
110+
111+
For instance, it will translate:
112+
113+
```javascript
114+
[1, 2, 3].map((n) => n + 1)
115+
```
116+
117+
Into:
118+
119+
```javascript
120+
"use strict";
121+
122+
[1, 2, 3].map(function (n) {
123+
return n + 1;
124+
});
125+
```
126+
109127
### Undefined everywhere!
110128

111129
```javascript
@@ -175,6 +193,17 @@ dog + 1 + "a"
175193
dog - 1
176194
```
177195

196+
Other gotchas:
197+
198+
```javascript
199+
// NaN is fun!
200+
console.assert(typeof NaN === "number");
201+
Object.is(NaN, NaN) // true
202+
NaN === NaN // false
203+
```
204+
205+
A good talk on the topic: [Wat](https://www.destroyallsoftware.com/talks/wat)
206+
178207
#### Always use triple comparators (`===`) instead of double (`==`)
179208

180209
```javascript
@@ -219,6 +248,9 @@ const source = { b: 2};
219248
const merged = Object.assign(target, source);
220249
console.assert(_.isEqual(merged, {a: 1, b:2});
221250

251+
// Spread operator
252+
const merge2 = {...target, ...source};
253+
222254
// Array.includes (ES7)
223255
const theArray = [1, 2]
224256
console.assert(theArray.includes(1))
@@ -234,6 +266,8 @@ The book [JavaScript: The Good Parts](https://www.oreilly.com/library/view/javas
234266
235267
> You make prototype objects, and then … make new instances. Objects are mutable in JavaScript, so we can augment the new instances, giving them new fields and methods. These can then act as prototypes for even newer objects. We don't need classes to make lots of similar objects… Objects inherit from objects. What could be more object oriented than that?
236268
269+
![JavaScript: the good parts](./img/js-the-good-parts.jpg)
270+
237271
Some good articles:
238272
239273
- [A Plain English Guide to JavaScript Prototypes](http://sporto.github.io/blog/2013/02/22/a-plain-english-guide-to-javascript-prototypes/)
@@ -519,6 +553,31 @@ const result = templateTag`hello ${1 + 2}`;
519553
console.assert(result === "hello 3");
520554
```
521555
556+
Here's an example with Styled Components:
557+
558+
```javascript
559+
const Button = styled.a`
560+
/* This renders the buttons above... Edit me! */
561+
display: inline-block;
562+
border-radius: 3px;
563+
padding: 0.5rem 0;
564+
margin: 0.5rem 1rem;
565+
width: 11rem;
566+
background: transparent;
567+
color: white;
568+
border: 2px solid white;
569+
570+
/* The GitHub button is a primary button
571+
* edit this to target it specifically! */
572+
${props => props.primary && css`
573+
background: white;
574+
color: black;
575+
`}
576+
`
577+
```
578+
579+
You can see how template tags and arrow functions lead to more concise code!
580+
522581
## Loops
523582
524583
### `for... of`
@@ -642,18 +701,22 @@ let nestedProp = obj.first?.second;
642701
- Is it a modern language?
643702
- Can we say that JavaScript does not have types?
644703
- What is the value of this expression: `"1" + "1"`? `3 + 2 + "5"`?
645-
- What should I use? `let`, `var`, or `cost`?
704+
- What should I use? `let`, `var`, or `const`?
646705
- How do you add support for modern JS features in older browsers?
647706
- How are variables scoped in JavaScript?
648707
- What should you watch for when comparing variables in JavaScript?
649-
- `const a = [1]; const b = [1];`: what does `b == b` evaluates to?
708+
- `const a = [1]; const b = [1];`: what does `a == b` evaluates to?
650709
- How do you write arrow functions?
651710
- `const {a} = {a: 1}`: what does `a` evaluate to?
652711

653712
Advanced:
654713

655-
- How does JavaScript "inherit" from each other?
656-
- If `const toaster = {name: 'Foo'}` and I want `const obj = {Foo: 1}`. How can I write this whatever the `toaster`'s `name`?
714+
- How does JavaScript objects "inherit" from each other?
715+
716+
```javascript
717+
// Write transform1 using a one-line arrow function with object structuring
718+
console.assert(_.isEqual(transform1({name: "Foo"}), {FooA:1}))
719+
console.assert(_.isEqual(transform1({name: "Bar"}), {Bar:1}))
657720
658721
Other assessments:
659722
29.7 KB
Loading

0 commit comments

Comments
 (0)