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:
Function.prototype.within = function (millis) { | |
var that = this; | |
return function () { | |
var args = Array.prototype.slice.call(arguments); // if sinful present, slice(arguments) | |
return window.setTimeout(function () { that.apply(null, args); }, millis); ); | |
}; | |
}; |
var EventEmitter = require("events").EventEmitter; | |
var emitter = new EventEmitter(); | |
var foo = function () { | |
console.log("foo"); | |
}; | |
emitter.once("foo", foo); | |
var bar = function () { |
/*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; | |
}; |
(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; | |
} |
// 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; | |
var promises = [ | |
async1(), | |
async2(), | |
… | |
asyncN() | |
]; | |
/* jQuery: warning, won't swallow exceptions */ | |
var deferred = $.Deferred(); | |
$.when.apply($, promises) | |
.done(function () { deferred.resolve(promises); }) |
I hereby claim:
To claim this, I am signing this object:
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)) { |
> 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) |
https://gist.github.com/ljharb/58faf1cfcb4e6808f74aae4ef7944cff
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.
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 mu