$(foo)
$(foo)
"Is this PHP?"
The tri-arrow function (AKA “the gun rack”), executes three times.
const booms = (boom = 'boom') ⇶ boom += ' ' + boom console.log(`${booms()} (I want you in my room)`)
(British Broadcasting Company)
JQuery
JQuery
React
JQuery
React
Angular
JQuery
React
Angular
Vanilla?
vanilla == plain // true
const a = document.querySelector(a);
const b = document.querySelector(b);
const c = document.querySelector(c);
const d = document.querySelector(d);
const e = document.querySelector(e);
const Ꮂ = n => document.querySelector(n)
Ꮂ('body').onclick = () => alert('wow!')
Honey and Candied Thyme?
const Ꮂ = n => document.querySelector(n)
Ꮂ('body').onclick = () => alert('wow!')
(that you copy/pasted from Stack Overflow)
(that you installed with npm)
Great for
interaction design
consultants
(like me!)
{{<demo>}}
<button aria-pressed="false">Toggle Me</button>
<style>
button {
border-width: 0 .25rem .25rem 0;
}
[aria-pressed="true"] {
border-width: .25rem 0 0 .25rem;
}
</style>
<script>
const toggle = document.querySelector('[aria-pressed]')
toggle.onclick = function () {
var pressed = this.getAttribute('aria-pressed') === 'true'
this.setAttribute('aria-pressed', !pressed)
}
</script>
{{</demo>}}
<button :aria-pressed="theState">Toggle me</button>
<button aria-pressed={theState}>Toggle me</button>
<button :aria-pressed="theState">Toggle me</button>
<button aria-pressed={theState}>Toggle me</button>
# Component name ## Recommended markup ## Recommended layout ## Recommended behavior ## Reference implementation
(plain / native)
<button is="toggle-button">Toggle me</button>
import Toggle from '../components/Toggle.js'
<button
is="toggle-button"
pressed={foo}
ontoggle={bar}>
Toggle me!
</button>
<button
is="toggle-button"
:pressed="foo"
v-on:toggle="bar">
Toggle me!
</button>
<script src="js/components.js" type="module">
# Markdown
<%template%>
<html>
get pressed() {
return this.hasAttribute('pressed')
}
set pressed(val) {
if (val) {
this.setAttribute('pressed', 'pressed')
this.setAttribute('aria-pressed', 'true')
} else {
this.removeAttribute('pressed')
this.setAttribute('aria-pressed', 'false')
}
}
static get observedAttributes() {
return ['pressed']
}
attributeChangedCallback(name) {
if (name === 'pressed') {
const event = new Event('toggle')
this.dispatchEvent(event)
}
}
onToggle="console.log(this.pressed)"
elem.pressed = true
elem.setAttribute('pressed', 'pressed')
2011
2018
109 KB → 399 KB
(1MB median)
“As much as I love JavaScript, it’s always the most expensive part of your site.”
— Addy Osmani, Google
(with custom properties + a bit of houdini)
"dependencies": {},
"devDependencies": {}
"dependencies": {},
"devDependencies": {}
demo?
CLI | "Empty" production build |
---|---|
Vue CLI | 106KB |
Preact CLI | 37.5KB |
probability(fraction) {
return Math.random() <= fraction
}
// TODO: A couple of for loops
function mutilator(obj, name = "m", context = window) {
const mutilated = {}
for (let prop in obj) {
let ref = `m-${prop}`
mutilated[ref] = obj[prop]
Object.defineProperty(mutilated, prop, {
set: function(v) {
this[ref] = v
context.dispatchEvent(
new CustomEvent(`${name}:${prop}`, {
detail: { value: v }
})
);
},
get: function() {
return this[ref]
}
});
}
return mutilated;
}
const mutable = mutilator({
string: 'foo',
num: 6,
etc: 'etc'
})
const mutable = mutilator({
string: 'foo',
num: 6,
etc: 'etc'
})
context.addEventListener('m:string', function(e) {
console.log(e.detail.value);
})
“Do stuff with JavaScript
if it's available”
“Do stuff with JavaScript
if it's available
AND YOU FUCKING NEED IT ”
<button is="toggle-button">Toggle me</button>
customElements.define(
'toggle-button',
ToggleButton,
{extends: 'button'}
);
youscroll.js
fluid-scroll.js
Scroll.js
pfSmoothScroll.js
smoothScroll.js
js-navbar-scroll.js
moveTo.js
AnimateScroll.js
scrollmenu.js
scrollToAnchor.js
body {
scroll-behavior: smooth;
}
body {
scroll-behavior: smooth;
}
Unrecognized?
Just skipped over
Meanwhile: JavaScript's error handling
JavaScript
JengaScript
Rendering On The Web
— Addy Osmani & Jason Miller
Can see the thing
Can actually use the thing
START
As long as 14 seconds!
Approx 200b IIFE, embedded: "Critical JavaScript"
Retweeted by... Brendan Eich
Inventor Of JavaScript
With special thanks to Tim Holman, Rob Dodson, Jason Miller, Nick Colley, Jen Simmons, Adam Silver, Christian Schaefer, Chris Ferdinandi, and Andy Bell for inspiration.