Run prettier everywhere
This commit is contained in:
@@ -17,11 +17,11 @@ console.log("hello");
|
||||
|
||||
```javascript
|
||||
// ???
|
||||
console.assert('1' == 1)
|
||||
console.assert("1" == 1);
|
||||
|
||||
// Better
|
||||
console.assert(!('1' === 1))
|
||||
console.assert('1' !== 1)
|
||||
console.assert(!("1" === 1));
|
||||
console.assert("1" !== 1);
|
||||
```
|
||||
|
||||
#### Comparing non-scalar
|
||||
@@ -29,22 +29,22 @@ console.assert('1' !== 1)
|
||||
Applied on arrays and objects, `==` and `===` will check for object identity, which is almost never what you want.
|
||||
|
||||
```javascript
|
||||
console.assert({a: 1} != {a: 1})
|
||||
console.assert({a: 1} !== {a: 1})
|
||||
console.assert({ a: 1 } != { a: 1 });
|
||||
console.assert({ a: 1 } !== { a: 1 });
|
||||
|
||||
const obj = {a: 1}
|
||||
const obj2 = obj
|
||||
console.assert(obj == obj2)
|
||||
console.assert(obj === obj2)
|
||||
const obj = { a: 1 };
|
||||
const obj2 = obj;
|
||||
console.assert(obj == obj2);
|
||||
console.assert(obj === obj2);
|
||||
```
|
||||
|
||||
Use a library such as [lodash](https://lodash.com/) to properly compare objects and array
|
||||
|
||||
```javascript
|
||||
import _ from 'lodash'
|
||||
import _ from "lodash";
|
||||
|
||||
console.assert(_.isEqual({a: 1}, {a: 1}))
|
||||
console.assert(_.isEqual([1, 2], [1, 2]))
|
||||
console.assert(_.isEqual({ a: 1 }, { a: 1 }));
|
||||
console.assert(_.isEqual([1, 2], [1, 2]));
|
||||
```
|
||||
|
||||
### `Object` methods
|
||||
@@ -62,34 +62,34 @@ console.assert(_.isEqual([1, 2], [1, 2]))
|
||||
### Objects
|
||||
|
||||
```javascript
|
||||
const toaster = {size: 2, color: 'red', brand: 'NoName'};
|
||||
const toaster = { size: 2, color: "red", brand: "NoName" };
|
||||
|
||||
// Get one object key
|
||||
const {size} = toaster;
|
||||
console.assert(size === 2)
|
||||
const { size } = toaster;
|
||||
console.assert(size === 2);
|
||||
|
||||
// Get the rest with ...rest
|
||||
const {color, brand, ...rest} = toaster;
|
||||
console.assert(_.isEqual(rest, {size: 2}));
|
||||
const { color, brand, ...rest } = toaster;
|
||||
console.assert(_.isEqual(rest, { size: 2 }));
|
||||
|
||||
// Set default
|
||||
const {size2 = 3} = toaster
|
||||
console.assert(size2 === 3)
|
||||
const { size2 = 3 } = toaster;
|
||||
console.assert(size2 === 3);
|
||||
|
||||
// Rename variables
|
||||
const {size: size3} = toaster
|
||||
console.assert(size3 === 2)
|
||||
const { size: size3 } = toaster;
|
||||
console.assert(size3 === 2);
|
||||
|
||||
// Enhances object literals
|
||||
const name = 'Louis'
|
||||
const person = {name}
|
||||
console.assert(_.isEqual(person, {name: 'Louis'}))
|
||||
const name = "Louis";
|
||||
const person = { name };
|
||||
console.assert(_.isEqual(person, { name: "Louis" }));
|
||||
|
||||
// Dynamic properties
|
||||
const person2 = {['first' + 'Name']: 'Olympe'}
|
||||
console.assert(_.isEqual(person2, {firstName: 'Olympe'}))
|
||||
const person2 = { ["first" + "Name"]: "Olympe" };
|
||||
console.assert(_.isEqual(person2, { firstName: "Olympe" }));
|
||||
// Btw, you can include quotes although nobody does this
|
||||
console.assert(_.isEqual(person2, {'firstName': 'Olympe'}))
|
||||
console.assert(_.isEqual(person2, { firstName: "Olympe" }));
|
||||
```
|
||||
|
||||
### Array
|
||||
@@ -107,29 +107,29 @@ console.assert(_.isEqualWith(rest, [3]));
|
||||
## `let` and `const`
|
||||
|
||||
```javascript
|
||||
const constantVar = 'a';
|
||||
const constantVar = "a";
|
||||
|
||||
// Raises "constantVar" is read-only
|
||||
constantVar = 'b';
|
||||
constantVar = "b";
|
||||
|
||||
let mutableVar = 'a';
|
||||
mutableVar = 'a';
|
||||
let mutableVar = "a";
|
||||
mutableVar = "a";
|
||||
|
||||
// Note: this will work ok
|
||||
const constantObject = {a: 1}
|
||||
constantObject.a = 2
|
||||
constantObject.b = 3
|
||||
const constantObject = { a: 1 };
|
||||
constantObject.a = 2;
|
||||
constantObject.b = 3;
|
||||
|
||||
// Raises: "constantObject" is read-only
|
||||
constantObject = {a: 1}
|
||||
constantObject = { a: 1 };
|
||||
|
||||
// const and let are block scoped. A block is enclosed in {}
|
||||
{
|
||||
const a = 'a';
|
||||
console.log({a})
|
||||
const a = "a";
|
||||
console.log({ a });
|
||||
}
|
||||
// Raises: ReferenceError: a is not defined
|
||||
console.log({a})
|
||||
console.log({ a });
|
||||
```
|
||||
|
||||
Note: try to use `const` as much as you can.
|
||||
@@ -152,29 +152,29 @@ The first advantage of arrow function is that they're shorter to write:
|
||||
|
||||
```javascript
|
||||
// You can define a function this way:
|
||||
const myFunction = function() {
|
||||
const myFunction = function () {
|
||||
console.log("hello world");
|
||||
}
|
||||
};
|
||||
|
||||
// With an arrow function, you save a few characters:
|
||||
const myArrowFunction = () => {
|
||||
console.log("hello world");
|
||||
}
|
||||
};
|
||||
|
||||
// Some things, like params parentheses, and function code brackets, are optional
|
||||
const myFunctionToBeShortened = function(a) {
|
||||
const myFunctionToBeShortened = function (a) {
|
||||
return a;
|
||||
}
|
||||
};
|
||||
|
||||
// Shorter arrow function
|
||||
const myFunctionToBeShortenedArrowV1 = (a) => {
|
||||
return a;
|
||||
}
|
||||
};
|
||||
|
||||
// Shortest arrow function
|
||||
// Remove single param parenthesis, remove function code bracket, remove return
|
||||
const myFunctionToBeShortenedArrowV2 = a => a
|
||||
console.assert(myFunctionToBeShortenedArrowV2(1) === 1)
|
||||
const myFunctionToBeShortenedArrowV2 = (a) => a;
|
||||
console.assert(myFunctionToBeShortenedArrowV2(1) === 1);
|
||||
```
|
||||
|
||||
### How `this` works in arrow functions
|
||||
|
||||
Reference in New Issue
Block a user