Skip to content

Instantly share code, notes, and snippets.

View WebReflection's full-sized avatar
🎯
Focusing

Andrea Giammarchi WebReflection

🎯
Focusing
View GitHub Profile
// a basic delegated EventListener
class DelegatedListener {
constructor(delegated) {
this.delegated = delegated;
}
handleEvent(event) {
this.delegated['on' + event.type](event);
}
// it's so simple to have any handler you need
class Clicker extends Handler {
onclick(event) {
alert(event.type); // click
}
}
// generic event handler
class Handler {
class TenClicks {
constructor(currentTarget) {
this.clicks = 10;
currentTarget.addEventListener('click', this);
}
handleEvent(event) {
this['on' + event.type](event);
}
// interface
var EventListener = {
handleEvent(e) {
this['on' + e.type](e);
}
};
// component
var Clicker = Object.create(EventListener, {
clicks: {writable: true, value: 0},
var obj = {
handleEvent(e) {
// always true
console.log(this === obj);
}
};
document.body.addEventListener('click', obj);
function whatAWaste(event) {
// BODY, HTML
console.log(event.currentTarget.nodeName);
// it is ALWAYS true !!!
console.assert(event.currentTarget === this);
}
document.documentElement.addEventListener('click', whatAWaste);
document.body.addEventListener('click', whatAWaste);
@WebReflection
WebReflection / benchmark-dom-classes.js
Last active November 13, 2021 20:52
Classes VS DOM Events Handling Benchmark
// Players
class ClickCounter {
constructor() { this.clicks = 0; }
onclick(e) { this.clicks += (e.type === 'click') ? 1 : -1; }
}
class Handler extends ClickCounter {
constructor(currentTarget) {
super();
currentTarget.addEventListener('click', this);
@WebReflection
WebReflection / randommac.sh
Last active March 20, 2018 13:27
Getting free wifi on Linux too
#!/usr/bin/env bash
# @example
# chmod a+x randommac.sh
# ./randommac.sh wlp2s0
# @credits original macOS / osX version via @rem
# https://remysharp.com/2017/05/29/getting-free-wifi
if [ "$1" != "" ]; then
"use strict";
class Foo {
constructor(root) {
this.render = hyperHTML.bind(root);
this.state = Introspected({}, $ctrl => this.update($ctrl));
this.state.items = [
{ id: 1, text: "foo" },
{ id: 2, text: "bar" },
// https://webreflection.medium.com/using-the-input-datetime-local-9503e7efdce
Date.prototype.toDatetimeLocal =
function toDatetimeLocal() {
var
date = this,
ten = function (i) {
return (i < 10 ? '0' : '') + i;
},
YYYY = date.getFullYear(),
MM = ten(date.getMonth() + 1),