View array_iteration_thoughts.md

While attempting to explain JavaScript's reduce method on arrays, conceptually, I came up with the following - hopefully it's helpful; happy to tweak it if anyone has suggestions.

Intro

JavaScript Arrays have lots of built in methods on their prototype. Some of them mutate - ie, they change the underlying array in-place. Luckily, most of them do not - they instead return an entirely distinct array. Since arrays are conceptually a contiguous list of items, it helps code clarity and maintainability a lot to be able to operate on them in a "functional" way. (I'll also insist on referring to an array as a "list" - although in some languages, List is a native data type, in JS and this post, I'm referring to the concept. Everywhere I use the word "list" you can assume I'm talking about a JS Array) This means, to perform a single operation on the list as a whole ("atomically"), and to return a new list - thus making it much simpler to think about both the old list and the new one, what they contain, and

View index.js
// Welcome! require() some modules from npm (like you were using browserify)
// and then hit Run Code to run your code on the right side.
// Modules get downloaded from browserify-cdn and bundled in your browser.
var url = require('url');
var qs = require('qs');
var assert = require('assert')
var parsed = url.parse('http://www.example.com/somepath?x=1');
var parsedQuery = qs.parse(parsed.query);
assert.deepEqual(parsedQuery, { x: 1 });
View test.js
> Object.defineProperty(Object.prototype, 'a', { set: function (v) { throw new Error(v); } });
{}
> Object.getOwnPropertyNames({})
[]
> Object.getOwnPropertyNames({a: 1})
Error: true
at Object.defineProperty.set (repl:1:74)
at Function.getOwnPropertyNames (native)
at repl:1:9
at REPLServer.self.eval (repl.js:110:21)
View grid.js
function formatValue(x) {
var ret;
if (typeof x == "string") {
return "'" + x.replace(/\n/g,"\\n") + "'";
}
if (typeof x == "number" && x === 0 && (1/x === -Infinity)) {
return "-0";
}
if (Array.isArray(x)) {
View keybase.md

Keybase proof

I hereby claim:

  • I am ljharb on github.
  • I am ljharb (https://keybase.io/ljharb) on keybase.
  • I have a public key whose fingerprint is F208 CF4D F9AF 4C98 82F5 C737 DD6C DDD3 CBBB C186

To claim this, I am signing this object:

View gist:8174372
var promises = [
async1(),
async2(),
asyncN()
];
/* jQuery: warning, won't swallow exceptions */
var deferred = $.Deferred();
$.when.apply($, promises)
.done(function () { deferred.resolve(promises); })
View gist:6321587
// because of declaration hoisting, wherever we put `filter()`, it
// will be declared for a longer lifetime than is necessary.
//
// inline function expression (or arrow function) inside the loop
// isn't the answer, because then you recreate the function over
// and over again.
function doSomething() {
var items = [], i, ret = 0;
View gist:6126127
(function ($) {
var keys = [], code = '38,38,40,40,37,39,37,39,66,65';
var konami = function (e) {
keys.push(e.which);
if (keys.length >= 11 && keys.slice(-11).toString() !== code) {
keys = [];
/* do your konami thing here */
} else if (keys.length > 11) {
keys.length = 11;
}
View Queue.js
/*global setTimeout */
var Queue = function Queue(q) {
"use strict";
// (C) WebReflection - Mit Style License
var callback,
next = function next() {
callback = q.shift();
if (callback) { callback(q); }
return !!callback;
};
View Listeners.js
var EventEmitter = require("events").EventEmitter;
var emitter = new EventEmitter();
var foo = function () {
console.log("foo");
};
emitter.once("foo", foo);
var bar = function () {