This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function car() { | |
return { | |
start: function() { | |
console.log("Engine on.") | |
}, | |
accelerate: function() { | |
console.log("Let's go!") | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const define = (object, ...pairs) => | |
(Array.isArray(pairs[0]) ? pairs : [pairs]).reduce((accumulator, pair) => { | |
const [ key, value ] = pair; | |
return Object.defineProperty(accumulator, key, { value, enumerable: true }); | |
}, object); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const toRGB = (() => { | |
const expand = hex => | |
hex.length < 7 ? hex.split("").reduce((a, b) => a + b + b) : hex; | |
const convert = hex => | |
hex.match(/[\d\w]{2}/g).map(val => parseInt(val, 16)); | |
return hex => { | |
const [r, g, b] = convert(expand(hex)); | |
return `rgb(${r}, ${g}, ${b})`; | |
}; | |
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const immutable = (() => { | |
const getValue = obj => key => obj[key]; | |
const isObject = obj => Object(obj) === obj; | |
const isMutable = obj => isObject(obj) && !Object.isFrozen(obj); | |
return obj => { | |
Object.keys(obj).map(getValue(obj)).filter(isMutable).forEach(immutable); | |
return Object.freeze(obj); | |
}; | |
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const clone = map => | |
new Map(JSON.parse(JSON.stringify([...map]))); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const odd = { a: 1, b: 3 }; | |
const even = Object.entries(odd).reduce((object, pair) => { | |
const [key, value] = pair; | |
object[key] = value + 1; | |
return object; | |
}, {}); | |
console.log(even); // => { a: 2, b: 4 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
document.body.innerHTML = "<hello-message>Jane</hello-message>"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
customElements.define("hello-message", class extends HTMLElement { | |
constructor() { | |
super(); | |
const root = this.attachShadow({mode: "open"}); | |
root.innerHTML = "Hello <slot></slot>"; | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
customElements.define("hello-message", class extends HTMLElement { | |
constructor() { | |
super(); | |
const root = this.attachShadow({mode: "open"}); | |
root.innerHTML = "Hello <slot></slot>"; | |
} | |
static get observedAttributes() { | |
return ["name"]; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const state = ["Buy milk", "Call Sarah", "Pay bills"]; | |
customElements.define("todo-item", class extends HTMLElement { | |
constructor() { | |
super(); | |
const root = this.attachShadow({mode: "open"}); | |
root.innerHTML = ` | |
<label> | |
<input type=checkbox> | |
<slot></slot> |