OSWeekends
Advanced JavaScript Patterns
Image provided by Maxi Abella
2019
@cesalberca / http://bit.ly/os2019-js
César Alberca
cesalberca@gmail.com
@cesalberca
@cesalberca
#OSWeekends #AdvancedJavaScriptPatterns
2
@cesalberca / http://bit.ly/os2019-js
Parámetros por defecto
3
@cesalberca / http://bit.ly/os2019-js
Parámetros por defecto I
export function multiply(a = 1, b = 1) {
return a * b
}
4
@cesalberca / http://bit.ly/os2019-js
Parámetros por defecto II
export function priceAfterTaxes(price, tax = multiply(21, 0.01)) {
return price + price * tax
}
5
@cesalberca / http://bit.ly/os2019-js
Parámetros por defecto III - Demo
6
@cesalberca / http://bit.ly/os2019-js
Tagged template
7
@cesalberca / http://bit.ly/os2019-js
Tagged template I
tag`Hello ${string}`
8
@cesalberca / http://bit.ly/os2019-js
Tagged template II
const tag = (strings, ...values) => strings[0] + values[0] + strings[1]
const string = 'world'
tag`Hello ${string}!` // Hello world!
9
@cesalberca / http://bit.ly/os2019-js
Tagged template III
html`<h1>Counter: ${counter}</h1>`
10
@cesalberca / http://bit.ly/os2019-js
Tagged template - Demo
11
@cesalberca / http://bit.ly/os2019-js
Proxies
12
@cesalberca / http://bit.ly/os2019-js
Proxies API
const handler = {
get(target, property, receiver) {},
set(target, property, value, receiver) {},
apply(target, thisArg, argumentsList) {},
construct(target, argumentsList, newTarget) {}
}
const proxy = new Proxy(target, handler)
13
@cesalberca / http://bit.ly/os2019-js
Proxies I
const person = createSafe({ company: { name: 'Autentia' }})
expect(() => { person.compani.name }).not.toThrowError()
14
@cesalberca / http://bit.ly/os2019-js
Proxies I - Demo
15
@cesalberca / http://bit.ly/os2019-js
Proxies II
const functionLogger = createLogger(capitalize, loggerStub)
functionLogger('test')
expect(loggerStub.log).toHaveBeenCalledWith(
'2018-10-10T12:34:56.000Z [function] capitalize (Args: test) {Result: Test}'
)
16
@cesalberca / http://bit.ly/os2019-js
Proxies II - Demo
17
@cesalberca / http://bit.ly/os2019-js
Patrón observador
18
@cesalberca / http://bit.ly/os2019-js
Patrón observador I - Sujeto
export class Subject {
constructor() {
this.observers = []
this.counter = 0
}�
addObserver(observer) {
this.observers.push(observer)
}�
notifyObservers() {
this.observers.forEach(observer => observer.notify(this))
}
...
...
incrementCounter() {
this.counter++
this.notifyObservers()
}
}
19
@cesalberca / http://bit.ly/os2019-js
Patrón observador I - Observador
export class Observer {
constructor(subject) {
this.subject = subject
this.subject.addObserver(this)
this.value = 0
}
display() {
return `Observer counter: ${this.value}`
}
...
…
notify(subject) {
this.value = subject.counter
}
}
20
@cesalberca / http://bit.ly/os2019-js
Patrón observador I - Test
const subject = new Subject()
const observer = new Observer(subject)
�subject.incrementCounter()
expect(observer.display()).toBe('Observer counter: 1')
21
@cesalberca / http://bit.ly/os2019-js
Patrón observador - Observador II
export class Subject {
set counter(value) {
this._counter = value
this.notifyObservers()
}
get counter() {
return this._counter
}
}
export class Subject {
set counter(value) {
this.#counter = value
this.notifyObservers()
}
get counter() {
return this.#counter
}
}
22
https://github.com/tc39/proposal-class-fields
@cesalberca / http://bit.ly/os2019-js
Patrón observador - Test II
const subject = new Subject()
const observer = new Observer(subject)
�subject.counter = 15
expect(observer.display()).toBe('Observer counter: 15')
23
@cesalberca / http://bit.ly/os2019-js
Vue vs React
this.counter = 1
this.array.push('foo')
this.setState({ counter: 1 })
this.setState({
array: [
...this.state.array,
newElement
]
})
24
@cesalberca / http://bit.ly/os2019-js
Observables
25
@cesalberca / http://bit.ly/os2019-js
Observables I
const person = createObservable({ name: 'John' })
const stub = jest.fn(() => person.name)
observe(stub)
person.name = 'Sara'
expect(stub).toHaveBeenCalled()
26
@cesalberca / http://bit.ly/os2019-js
Observables I - Demo
27
@cesalberca / http://bit.ly/os2019-js
Funcional
28
@cesalberca / http://bit.ly/os2019-js
Funcional I
const exclamation = string => `${string}!`
const dash = string => string.split(' ').map(word => word.split('').join('-')).join(' ')
const upperCase = string => string.toUpperCase()
29
@cesalberca / http://bit.ly/os2019-js
Funcional II
exclamation(dash(upperCase('fus roh dah')))
30
@cesalberca / http://bit.ly/os2019-js
Funcional III
export const pipe = (...fns) => x => fns.reduce((g, f) => f(g), x)
31
@cesalberca / http://bit.ly/os2019-js
Funcional IV
const upperCasedDashedExclamation = pipe(
upperCase,
dash,
exclamation
)
upperCasedDashedExclamation('fus roh dah')
32
@cesalberca / http://bit.ly/os2019-js
Funcional V
const safeObservableLogger = pipe(
createLogger,
createObservable,
createSafe
)
33
@cesalberca / http://bit.ly/os2019-js
React vs Vue vs Angular vs ...
34
RamboJS
@cesalberca / http://bit.ly/os2019-js
35
empleo@autentia.com https://www.autentia.com
Contratando
Referencias
36
@cesalberca / http://bit.ly/os2019-js
César Alberca
cesalberca@gmail.com
@cesalberca
@cesalberca
#OSWeekends #AdvancedJavaScriptPatterns
37
@cesalberca / http://bit.ly/os2019-js