Skip to content

Instantly share code, notes, and snippets.

@ajklein
ajklein / case-insensitive-set.js
Last active January 17, 2019 23:29
Case insensitive Set
class CaseInsensitiveSet extends Set {
add(key) {
return super.add(key.toLowerCase());
}
delete(key) {
return super.delete(key.toLowerCase());
}
has(key) {
@ajklein
ajklein / parameter-eval.js
Last active November 28, 2017 22:45
Parameter scoping and sloppy eval
// What does this code do?
function f(x = eval("var z = 2; 1"), y = z) {
return [x, y, z];
}
f();
// Spec says to throw a ReferenceError, since 'z' is not visible either
// to other parameters in the list, or to the function body.
//
// Results in various engines:
@ajklein
ajklein / split.md
Last active August 29, 2015 14:26
String.prototype.split and its `limit` argument

String.prototype.split ( separator, limit )

Splits the receiver at separator, returns an Array of at most limit segments.

ES5 (15.5.4.14.5): If limit is undefined, let lim = 232-1; else let lim = ToUint32(limit).

ES6 (21.1.3.17.8): If limit is undefined, let lim = 253-1; else let lim = ToLength(limit).

Two problems

  • Return value is an Array, so a limit greater than 232-1 would result in a "malformed" array (one with elements past the end of the array). Iteration over the return value will skip all such elements.