Skip to content

Commit b464979

Browse files
committed
Added episode9 source
1 parent dce4255 commit b464979

File tree

3 files changed

+128
-0
lines changed

3 files changed

+128
-0
lines changed

episode9/README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Immutability in JavaScript
2+
3+
ReactCasts, episode 9.
4+
5+
In this episode I'll talk about why immutability is important and how it can benefit you. I will draw some comparisons between JavaScript (which doesn't treat data as immutable by default) and programming languages that have immutability built in. Finally, I will show how to make immutable operations in plain Javascript.
6+
7+
Screencast video:
8+
https://youtu.be/4LzcQyZ9JOU
9+
10+
# Outline
11+
12+
- What is Immutability and why it's important.
13+
- JavaScript non-destructive Array methods
14+
- Spread Operator
15+
- External Libraries
16+
17+
18+
# Build & Run Instructions
19+
20+
1. To build and run the code in this directory, ensure you have [npm](https://www.npmjs.com) installed
21+
22+
2. Install
23+
```
24+
npm install
25+
```
26+
27+
3. Start the application
28+
```
29+
npm start
30+
```

episode9/index.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
const houses = ['Arryn', 'Frey', 'Greyjoy', 'Stark', 'Lannister', 'Tyrell'];
2+
3+
// There are a few non-destructive methods available for arrays.
4+
// One example is slice. It returns a piece of a given Array
5+
// without modifiyng the original one.
6+
7+
console.log(houses.slice(0, 4)); // ['Arryn', 'Frey', 'Greyjoy', 'Stark']
8+
9+
console.log(houses); // ['Arryn', 'Frey', 'Greyjoy', 'Stark', 'Lannister', 'Tyrell']
10+
11+
12+
13+
// Besides slice, there are other and useful array methods
14+
// such as filter, map and reduce that also returns new arrays
15+
// instead of modifying the original one.
16+
17+
const direwolves = [
18+
{ name: 'Ghost', alive: true },
19+
{ name: 'Nymeria', alive: true },
20+
{ name: 'Lady', alive: false },
21+
{ name: 'Grey Wind', alive: false },
22+
{ name: 'Shaggydog', alive: false },
23+
{ name: 'Summer', alive: false }
24+
]
25+
26+
const wolves = direwolves.filter(wolf => wolf.alive)
27+
28+
console.log(direwolves); //Full Array
29+
console.log(wolves); // [{name:'Ghost', alive: true},{name:'Nymeria', alive:true}]
30+
31+
32+
33+
// Javascript ES6 introduces a new operator called the spread operator.
34+
// The Spread operator provides an easy way to create a new array
35+
// by copying values from another array.
36+
37+
const completeHouses = [...houses, 'Targaryen'];
38+
console.log(houses) // ['Arryn','Frey','Greyjoy','Stark','Lannister','Tyrell']
39+
console.log(completeHouses) //['Arryn','Frey','Greyjoy','Stark','Lannister','Tyrell','Targaryen']
40+
41+
// There is a proposal for object spread syntax for the next version
42+
// of the JS language, and you can use it right now with Babel.
43+
44+
const state = {
45+
name: 'Jon Snow',
46+
occupation: 'Lord Commander',
47+
skills: [] // knows nothing...
48+
}
49+
50+
// Now, using the spread operator I will copy the original state objects keys
51+
// and values, while at the same changing the occupation value.
52+
53+
const newState = { ...state, occupation: 'King in the North' };
54+
console.log(newState);
55+
// {name:'Jon Snow', occupation:'King in the North', skills:[]}
56+
console.log(state);
57+
// {name:'Jon Snow', occupation:'Lord Commander', skills:[]}
58+
59+
60+
// The spread operator makes shallow copies, which means
61+
// it only goes one level deep while copying.
62+
// If you want to update the array, you will need to make it
63+
// in an immutable fashion as well:
64+
65+
newState.skills = [...state.skills, 'fighting'];
66+
console.log(newState);
67+
// {name:'Jon Snow', occupation:'King in the North', skills:[fighting]}
68+
console.log(state);
69+
// {name:'Jon Snow', occupation:'Lord Commander', skills:[]}

episode9/package.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "reactcasts-episode9",
3+
"version": "0.1.0",
4+
"description": "",
5+
"main": "index.js",
6+
"quokka": {
7+
"babel": {
8+
"env": "development"
9+
}
10+
},
11+
"scripts": {
12+
"start": "NODE_ENV=development babel-node index.js",
13+
"test": "echo \"Error: no test specified\" && exit 1"
14+
},
15+
"repository": {
16+
"type": "git",
17+
"url": "git+https://github.com/wallabyjs/quokka-babel-import-sample.git"
18+
},
19+
"author": "Cassio Zen",
20+
"license": "ISC",
21+
"bugs": {
22+
"url": "https://github.com/wallabyjs/quokka-babel-import-sample/issues"
23+
},
24+
"homepage": "https://github.com/wallabyjs/quokka-babel-import-sample#readme",
25+
"devDependencies": {
26+
"babel-cli": "^6.24.0",
27+
"babel-preset-react-app": "^2.2.0"
28+
}
29+
}

0 commit comments

Comments
 (0)