|
| 1 | +# Javascript ES6 |
| 2 | + |
| 3 | +These are some of the concepts of Javascript you will need to master. These will help you understand React better. |
| 4 | + |
| 5 | +## Let and Const |
| 6 | + |
| 7 | +JavaScript has always had `var`: |
| 8 | + |
| 9 | +```js |
| 10 | +var name = "MRND"; |
| 11 | +``` |
| 12 | + |
| 13 | +`var` can be hard to manage especially because of it's "function scoping", so now we've got two other ways to define values that have "block scope": |
| 14 | + |
| 15 | +```js |
| 16 | +// var does not have block scope |
| 17 | +var name = "MRND"; |
| 18 | +if (true) { |
| 19 | + var name = "Summer"; |
| 20 | + name; // 'Summer' |
| 21 | +} |
| 22 | +name; // 'Summer' |
| 23 | + |
| 24 | +// let has block scope |
| 25 | +let name = "MRND"; |
| 26 | +if (true) { |
| 27 | + let name = "Summer"; |
| 28 | + name; // 'Summer' |
| 29 | +} |
| 30 | +name; // 'MRND' |
| 31 | + |
| 32 | +// const has block scope too |
| 33 | +const name = "MRND"; |
| 34 | +if (true) { |
| 35 | + let name = "Summer"; |
| 36 | + name; // 'Summer' |
| 37 | +} |
| 38 | +name; // 'MRND' |
| 39 | + |
| 40 | +// let can be reassigned |
| 41 | +let isOpen = true; |
| 42 | +isOpen = false; |
| 43 | +isOpen; // false |
| 44 | + |
| 45 | +// const cannot be reassigned |
| 46 | +const isOpen = true; |
| 47 | +isOpen = false; // throws error |
| 48 | +``` |
| 49 | + |
| 50 | +We find block scope to make more sense to people and is generally more useful, therefore we don't use `var`. |
| 51 | + |
| 52 | +We use `const` for everything, unless it can be reassigned later, only then do we use `let`. It's a way to let other people know (pun intended) to watch out for that value, because it will likely change over time. |
| 53 | + |
| 54 | +In practice, nearly everything is `const`. |
| 55 | + |
| 56 | +## String templates |
| 57 | + |
| 58 | +```js |
| 59 | +const something = "ugly stuff"; |
| 60 | +const str = "instead of " + something + " like this"; |
| 61 | + |
| 62 | +const something = "lovely stuff"; |
| 63 | +const str = `you can do ${something} like this`; |
| 64 | + |
| 65 | +const str = ` |
| 66 | + also |
| 67 | + multiline |
| 68 | + is totally cool |
| 69 | +`; |
| 70 | +``` |
| 71 | + |
| 72 | +## Concise object methods |
| 73 | + |
| 74 | +You can drop off `: function` from object method definitions. |
| 75 | + |
| 76 | +```js |
| 77 | +const obj = { |
| 78 | + insteadOfThis: function() { |
| 79 | + // do stuff |
| 80 | + }, |
| 81 | + |
| 82 | + youCanDoThis() { |
| 83 | + // do stuff |
| 84 | + } |
| 85 | +}; |
| 86 | +``` |
| 87 | + |
| 88 | +## Arrow functions |
| 89 | + |
| 90 | +Arrow functions remove the context from a function, meaning the function has no `this`. If you reference `this` inside an arrow function, you get the `this` from outside the function. It also looks great. |
| 91 | + |
| 92 | +```js |
| 93 | +const obj = { |
| 94 | + url: "/api/stuff", |
| 95 | + |
| 96 | + fetch(users) { |
| 97 | + users.forEach(user => { |
| 98 | + // `this` is the `this` from outside this function because |
| 99 | + // there is no context inside an arrow function |
| 100 | + getUser(`${this.url}/${user.id}`); |
| 101 | + }); |
| 102 | + } |
| 103 | +}; |
| 104 | +``` |
| 105 | + |
| 106 | +Also, if the other side of an arrow function is an expression, it acts like an implicit return: |
| 107 | + |
| 108 | +```js |
| 109 | +const add = function(x, y) { |
| 110 | + return x + y; |
| 111 | +}; |
| 112 | + |
| 113 | +// becomes |
| 114 | +const add = (x, y) => { |
| 115 | + return x + y; |
| 116 | +}; |
| 117 | + |
| 118 | +// which can be shorter with explicit expression return |
| 119 | +const add = (x, y) => x + y; |
| 120 | + |
| 121 | +// if we want multiline, we can create an expression with () |
| 122 | +const add = (x, y) => x + y; |
| 123 | +``` |
| 124 | + |
| 125 | +## Arrays |
| 126 | + |
| 127 | +We do a lot with arrays, here are a few methods we use often: |
| 128 | + |
| 129 | +```js |
| 130 | +const numbers = [1, 2, 3, 4, 5]; |
| 131 | + |
| 132 | +// map converts an array to a new, transformed array |
| 133 | +const doubled = numbers.map(number => { |
| 134 | + return number * 2; |
| 135 | +}); |
| 136 | +doubled; // [ 2, 4, 6, 8, 10 ] |
| 137 | + |
| 138 | +// filter, return false to remove from an array |
| 139 | +const lessThan3 = numbers.filter(n => { |
| 140 | + return n < 3; |
| 141 | +}); |
| 142 | +lessThan3; // [ 1, 2 ] |
| 143 | + |
| 144 | +// remember, that can be super short |
| 145 | +const lessThan3 = numbers.filter(n => n < 3); |
| 146 | +``` |
| 147 | + |
| 148 | +## Destructuring |
| 149 | + |
| 150 | +```js |
| 151 | +const obj = { x: 1, y: 2 }; |
| 152 | + |
| 153 | +// instead of: |
| 154 | +const x = obj.x; |
| 155 | +const y = obj.y; |
| 156 | + |
| 157 | +// we can "destructure" the values off |
| 158 | +const { x, y } = obj; |
| 159 | +x; // 1 |
| 160 | +y; // 2 |
| 161 | + |
| 162 | +// you can use this all over the place, like function parameters |
| 163 | +function add({ x, y }) { |
| 164 | + return x + y; |
| 165 | +} |
| 166 | +add({ x: 3, y: 4 }); // 7 |
| 167 | +``` |
| 168 | + |
| 169 | +## Modules |
| 170 | + |
| 171 | +```js |
| 172 | +// instead of cjs |
| 173 | +var React = require("react"); |
| 174 | + |
| 175 | +// we use ES modules |
| 176 | +import React from "react"; |
| 177 | +import ReactDOM from "react-dom"; |
| 178 | + |
| 179 | +// and with destructuring to boot! |
| 180 | +import { render } from "react-dom"; |
| 181 | +``` |
| 182 | + |
| 183 | +_The contents of this file are picked from https://github.com/ReactTraining/react-workshop |
| 184 | +Have a look if you are curious. |
| 185 | +_ |
0 commit comments