JavaScript
CHALLENGE
const output = [];
output.push(typeof foo);
try {
output.push(typeof bar);
} catch (e) {
output.push(e.message);
}
var foo = () => 'foo';
let bar = () => 'bar';
console.log(output.join(' | '));
812 · Sondage
cliquez pour afficher
862 · cliquez pour afficher
JavaScript
CHALLENGE
const arr = [1, [2, 3], [4, [5, 6]]];
const a = arr.flat();
const b = arr.flat(Infinity);
const c = arr.flatMap(x => Array.isArray(x) ? x : [x, x]);
console.log(a, b, c);
533 · Sondage
cliquez pour afficher
536 · cliquez pour afficher
JavaScript
CHALLENGE
function* inner() {
const x = yield 1;
yield x + 1;
return 100;
}
function* outer() {
const result = yield* inner();
yield result;
}
const g = outer();
console.log(g.next().value, g.next(10).value, g.next().value, g.next().value);
❤1
1.3K · JavaScript
CHALLENGE
const obj = {
value: 42,
getValue: function() {
return this.value;
}
};
const unboundGetValue = obj.getValue;
const boundGetValue = unboundGetValue.bind(obj);
const boundTwice = boundGetValue.bind({ value: 100 });
console.log(
boundGetValue(),
boundTwice(),
unboundGetValue.call({ value: 7 }),
obj.getValue.apply({ value: 99 })
);
1.2K · Photo
cliquez pour afficher
cliquez pour afficher
💻 Deno is Joining Cloudflare (But…)
Maybe David Bushell saw the writing on the wall, as the Deno team isn't joining Cloudflare to keep working on Deno, but to fold celld into workerd so Durable Objects can be self-hosted (more on that here). The Deno runtime will get a final year of official fixes, Deno Deploy closes in six months, and JSR moves to Cloudflare's infrastructure.
Ryan Dahl
❤2👍1🤔1
1 · 1.1K · JavaScript
CHALLENGE
class EventBus {
#listeners = new Map();
on(event, cb) {
if (!this.#listeners.has(event)) this.#listeners.set(event, []);
this.#listeners.get(event).push(cb);
return this;
}
emit(event, ...args) {
const cbs = this.#listeners.get(event) || [];
return cbs.map(cb => cb(...args));
}
}
const bus = new EventBus();
bus.on('data', x => x * 2).on('data', x => x + 1);
const results = bus.emit('data', 5);
console.log(results, bus.emit('missing', 1));
❤3
1 · 1.1K · Photo
cliquez pour afficher
cliquez pour afficher
🤟 You Don’t Know Node.js EventLoop (30 min read)
You might be wondering if the title of this article is a bit ambitious, but fear not! I’ve put in the time and effort to cover everything you need to know about the EventLoop in Node.js. And believe me, you’ll discover so many new things along the way!
nairihar
❤6👍1
5 · 1.1K · JavaScript
CHALLENGE
const x = 0;
const y = '';
const z = null;
const r1 = x ?? y ? 'A' : 'B';
const r2 = z ?? y ? 'C' : 'D';
console.log(r1, r2);
903 · JavaScript
CHALLENGE
async function run() {
const results = await Promise.allSettled([
Promise.resolve(1),
Promise.reject('error'),
new Promise(resolve => setTimeout(() => resolve(3), 0)),
]);
console.log(results.map(r => r.value ?? r.reason).join(','));
}
run();
1 · 741 · Sondage
cliquez pour afficher
758 · cliquez pour afficher
JavaScript
CHALLENGE
class Counter {
#count = 0;
get value() {
return this.#count;
}
set value(v) {
if (v < this.#count) throw new RangeError('Cannot decrease');
this.#count = v;
}
}
const c = new Counter();
c.value = 5;
let msg;
try {
c.value = 3;
} catch (e) {
msg = e.message;
}
c.value = 10;
console.log(msg, c.value);
413 · Sondage
cliquez pour afficher
411 · cliquez pour afficher
JavaScript
CHALLENGE
const obj = { b: 1, 2: 'two', a: 3, 1: 'one' };
console.log(Object.keys(obj).join(','), Object.values(obj).join(','));
1 · 859 · JavaScript
CHALLENGE
class Counter {
static #total = 0;
#id;
constructor() {
Counter.#total++;
this.#id = Counter.#total;
}
get id() { return this.#id; }
static get total() { return Counter.#total; }
}
class Special extends Counter {
#label;
constructor(label) {
super();
this.#label = label;
}
get label() { return `${this.#label}-${this.id}`; }
}
const c1 = new Counter();
const c2 = new Special('x');
const c3 = new Special('y');
console.log(c1.id, c2.id, c3.label, Counter.total);
660 · Sondage
cliquez pour afficher
668 · cliquez pour afficher
JavaScript
CHALLENGE
const source = {
count: 0,
get value() {
this.count++;
return this.count;
}
};
const { value, ...rest } = source;
const spread = { ...source };
console.log(value, spread.value, source.count, JSON.stringify(rest));
200 · Sondage
cliquez pour afficher
198 · cliquez pour afficher