Skip to content

Instantly share code, notes, and snippets.

Keybase proof

I hereby claim:

  • I am bakkot on github.
  • I am bakkot (https://keybase.io/bakkot) on keybase.
  • I have a public key whose fingerprint is FD44 B8F2 65AC E177 48C5 23C8 A07A 119B 504C 8D8F

To claim this, I am signing this object:

let frame = document.body.appendChild(document.createElement('iframe'));
frame.addEventListener('load', ()=>{
let w = frame.contentWindow;
let evt;
w.addEventListener('error', e=>{evt = e;});
let scr = w.document.createElement('script');
scr.text = 'throw 42;';
var p = () => console.log(f);
{
p(); // undefined
console.log(f); // function f(){}
f = 1;
p(); // undefined
console.log(f); // 1
@bakkot
bakkot / es2016..es2017.md
Last active May 4, 2017 21:03
normative es2017 changes
  1. d28ebe6d Normative: Specify Date.UTC when called with fewer than two arguments (#642)

  2. 0916d813 Normative: Use explicit comparison to compare two lists in Valid Chosen Reads

  3. 90ed6ce2 Normative: Call NumberToRawBytes instead of the conversion operation to get the byte values list in Atomics.compareExchange

  4. 7949fae4 Normative: Only check for undefined count parameter in Atomics.wake, because it's not marked as optional

  5. a4d0e490 Normative: Unconditionally call ToNumber on the timeout parameter in Atomics.wait, because it's not marked as optional

@bakkot
bakkot / globals.md
Last active November 29, 2018 20:55

globalThis

  • PRO: almost shipping in Chrome, maybe used elsewhere
  • PRO: is actually correct
  • CON: might confuse JS beginners

Global

  • PRO: maybe less likely to confuse beginners
  • PRO/CON: looks like a namespace
  • CON: requires even more churn on this proposal

Optionally async functions

Or: two-color functions.

The problem: async is infectious.

There's a famous essay about this, called What Color is your Function, which I recommend reading.

For example, say you're writing a bundler. It needs to fetch resources, but is agnostic to how those resources are fetched, so it takes a readResource function from its caller. In some contexts the caller might have those resources synchronously available; in others not. That is, caller might have a synchronous readResource which returns a resource immediately, or an async readResources which returns a promise for a resource, which will need to be unwrapped with await.

@bakkot
bakkot / parseargs-subcommands.js
Last active September 23, 2022 00:32
how to implement subcommands with node's parseArgs argument parser
'use strict';
let { parseArgs } = require('util');
let mainOptions = { help: { type: 'boolean', short: 'h' } };
let subCommands = {
__proto__: null,
foo: fooCommand,
bar: barCommand,
};
@bakkot
bakkot / horrifying.js
Last active December 17, 2022 18:02
Various really bad JavaScript
(function x(){"use strict"; x = 1;}()); // TypeError
(function x(){x = 1; return x !== 1;}()); // write fails silently; function returns true
(function x(){"use strict"; x = (function(){throw 0;})();})() // Error 0
// These three lines rely on ES6.
(function x(){const x = 1;})() // No-op. In particular, not a redeclaration of x.
(function (){"use strict"; const x = 1; x = 2;})() // TypeError
(function (){const x = 1; x = 2;})() // TypeError. contrast (function x(){x = 2;}());
import fs from 'fs';
import { inspect } from 'util';
import path from 'path';
import { fileURLToPath } from 'url';
let __dirname = path.dirname(fileURLToPath(import.meta.url));
let usersToUpgrade = ['@justingrant:matrix.org'];
let targetPowerLevel = 50;
let roomId = '!nSLHQtIRJQxUJbEuGt:matrix.org'; // editor channel; this ID can be obtained from room settings -> Advanced