const obj = {
[action.name]: action.payload
};
state = {
hot: false,
cool: true,
destructured: 'destructured'
}
const { destructured, cool } = this.state
`Element is ${this.state.hot} and ${destructured} and ${cool}`
const { a,
b,
c: { n } = {} // provide a default in case it doesnt exist :)
} = obj
const { a,
b,
c: [ n ] = [] // provide a default in case it doesnt exist :)
} = obj
const result = {
location: 'Köln',
categories: [
{
name: 'eisdielerina',
adresse: 'bonner str',
type: 'gastro'
}
]
}
const getCategoryName = result => {
let { categories: [{name}] } = result
return name
}
getCategoryName(result) // 'eisdielerina'
const getCategoryName = ({categories: [{name}]}) => name
import React, { Fragment } from "react";
import ChatMessage from "../../Molecules/ChatMessage/ChatMessage";
export const Conversation = ({ senderId, messages }) => {
const getMessageContent2 = ({ parts : [{payload : {content}} = {}] = []}) => content
const isReceiver = id => senderId === id ? 'left' : 'right'
return (
<Fragment>
{ messages.map((message,i) =>
<ChatMessage key={i}
position={isReceiver(message.senderId)}
userName={message.senderId}
createdAt={message.createdAt}
text={getMessageContent2(message)}/>
)
}
</Fragment>
)
}
export default Conversation
const getMessageParts = ({ parts = [] }) => parts
const getMessagePayLoad = ({ payload = {} }) => payload
const getMessageContent = message => getMessageParts(message).map(parts => getMessagePayLoad(parts).content)
const stuff = { x: 'x', y: 'y', z: 'z'}
const { x:anotherName } = stuff
console.log(anotherName)
Better Name?
let {x: ten, girl: problem, solution: SOLUTION} = {x: 10, girl: 'grill', problem: 'chill'};
console.log(ten, problem, SOLUTION); //10, grill, chill
const { wat = 'wat', active = false } = { wat:0 }
const illbeZeroIfYouDontGiveAParam = (n = 0) => n;
// => 0
const funWithDefault = ({ initialValue = 0, isActive = false } = {}) => {
console.log(`initialValue: ${initialValue} isActive: ${isActive}`)
}
funWithDefault({}) // => 0, false
funWithDefault({initialValue: 1}) // => 1, false
funWithDefault({initialValue: 1, isActive: true}) // => 1, true
const foo = () => ({ a:1, b:2, c:3 })
const {a : first, b} = foo() || {};
console.log(first) // 1
Alternative with Object Ref
const foo = () => ({ a:1, b:2, c:3 })
let retObj;
const {a : first, b} = retObj = foo() || {};
const [a,b] = [1,2] // Position does matter
console.log(a,b) // => 1,2
const getTail = (head, ...tail) => tail;
getTail(1, 2, 3); // [2, 3]
const getHead = arr => head(arr) // 1 ( ͡° ͜ʖ ͡°)
const shiftToLast = (head, ...tail) => [...tail, head];
shiftToLast(1, 2, 3); // [2, 3, 1]
left side | right side
assignment context | declaration context
...gather = ...spread
const f = () => [1,2,0,3]
var [
a = 1,
b,
c,
...args
] = f() || [] // in return is null we stil get an empty Array
We can also destructure into object properties
const f = () => [1,2,0,3]
const ob = {}
var [
ob.a = 1,
ob.b,
ob.c,
...ob.args
] = f() || [] // in return is null we stil get an empty Array
Examples:
Dump:
const ar = [1,2,3]
let d;
[ d, d, ...a] = [0, ...a, 4] // a => 2,3,4
//similar to
[ , , ...a]
function tralala () {
{ // scope
let id;
}
}
'Interruptable/pausable' functions
Syntactic form to declaring a state machine
Localized blocking (only inside the generator, not the entire program)
Sync: A way to produce an iterator
It's okay not to consume a generator fully, they don't have to finish and will get garbage collected
function* gen() {
console.log('Hello')
yield // The generator decides when to pause
console.log('World')
}
var it = gen() // none of the generation runs, it just produces an iterator
it.next() // Hello
it.next() // World
for of loop
Remove any duplicates from an array of primitives (Rrder is preserved)
[... new Set(arr)]