Skip to content

Instantly share code, notes, and snippets.

View domfarolino's full-sized avatar

Dominic Farolino domfarolino

View GitHub Profile
@domfarolino
domfarolino / forEachObject.js
Created December 12, 2016 21:17
Demonstrating how Array.prototype.forEach(...) works by using it on Objects
Object.prototype["0"] = "test0";
let obj = {"1": "test1", "2": "test2"};
for (let k in obj) console.log(k); // 1, 2, 0
Object.defineProperty(obj, 'length', {
value: 3,
enumerable: false,
writable: true,
configurable: false
class CoolButton extends HTMLButtonElement {
constructor() {
super(); // must call as usual
}
connectedCallback() {
this.addEventListener('click', this._onClick);
// Woohoo, we've been connected
}
<link rel="stylesheet" href="styles.css"> <!-- For the daring (Chrome 54+) -->
<div class="card">
<slot name="card-header">Fallback Header</slot>
<slot name="card-body">Fallback card-body text</slot>
<p class="card-footer">
Written by <slot name="card-author"></slot>
</p>
</div>
:host {
font-family: sans-serif;
}
div.card {
width: 100%;
max-width: 300px;
display: inline-block;
box-shadow: 0px 1px 3px rgba(0, 0, 0, .6);
margin: 10px;
<cool-card>
<h2 slot="card-header">Card Header</h2>
<p slot="card-body">
"Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt."
<p slot="card-author">
<a href="https://twitter.com/domfarolino">By domfarolino</a>
</p>
</cool-card>
class CoolCard extends HTMLElement {
constructor() {
// MUST to allow HTMLElement interface to set
// itself up so we can take advantage of it
super();
}
connectedCallback() {
// Every time a cool-card gets inserted into a
// document this asynchronous callback gets called
@domfarolino
domfarolino / asyncForEach.js
Created November 28, 2016 16:20
Demonstrating how asynchronous code can let the browser render the UI in between propagated async callbacks
const array = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1];
function handleArrayValue(v) {
console.log(`Handling ${v}`);
for (let i = 0; i < 15000; ++i) {
for (let j = 0; j < i; ++j) {
// Nothing!
}
}
}
@domfarolino
domfarolino / lexThis.js
Created November 28, 2016 05:00
Simple es6 lexical this vs that=this/.bind(...) example
function CoolObject() {
var that = this;
// Allows us to use lexical this as opposed to .bind or that = this trick
this.coolPromise = new Promise((resolve, reject) => {
console.log(this === that);
setTimeout(() => {
resolve(10);
}, 2000);
});
@domfarolino
domfarolino / softBindPolyfill.js
Created November 14, 2016 00:48
YDKJS Soft Bind Polyfill
/**
* The softBind "polyfill" is kind of like .bind(..) hard binding
* however it is slightly more flexible in that it can allow for
* `this` overrides with implicit, explicit, and `new` bindings.
* The default .bind(..) is less flexible only allows `this` overrides
* in situations where `new` is used.
*
* To use it, you pass in some default value for `this`. This value
* will only be used if the value of `this` at runtime is the global
* `window` object, or undefined (possibly strict mode). This means
@domfarolino
domfarolino / EventFlow.md
Last active February 26, 2022 19:14
JavaScript Event Flow - A better understanding

This gist is deprecated and now exists here

This article is intended to further your knowledge of the various phases a DOM event goes through.

Great resources to refer to:

TL;DR