What is the output?
Anonymous Quiz
25%
1 2 hi 2 2
32%
1 undefined hi 2 1
33%
1 undefined undefined undefined 1
9%
1 2 undefined undefined 1
❤4
CHALLENGE
const wm = new WeakMap();
class Counter {
#id;
static #nextId = 0;
constructor() {
this.#id = Counter.#nextId++;
wm.set(this, { count: 0, id: this.#id });
}
inc() {
const data = wm.get(this);
data.count++;
return data.count;
}
}
const a = new Counter();
const b = new Counter();
a.inc(); a.inc();
b.inc();
console.log(`${a.inc()} ${b.inc()} ${wm.has(a)} ${wm.get(a).id} ${wm.get(b).id}`);
❤1👍1
What is the output?
Anonymous Quiz
24%
4 2 true 1 0
33%
2 1 true 0 1
22%
3 2 false 0 1
22%
3 2 true 0 1
CHALLENGE
const createCounterModule = (() => {
let instances = 0;
return function() {
instances++;
let count = 0;
return {
increment() { count++; return count; },
id: instances
};
};
})();
const a = createCounterModule();
const b = createCounterModule();
console.log(a.increment(), a.increment(), b.increment(), a.id, b.id);
export {};❤3
CHALLENGE
const base = {
greet() { return `Hello, ${this.name}`; }
};
const mid = Object.create(base);
mid.name = 'Mid';
const child = Object.create(mid);
child.name = 'Child';
console.log(
child.greet(),
mid.greet(),
Object.getPrototypeOf(child) === mid,
child.hasOwnProperty('greet')
);❤3
What is the output?
Anonymous Quiz
29%
Hello, Child Hello, Mid true false
43%
Hello, Child Hello, Mid false true
26%
Hello, Mid Hello, Mid false false
3%
Hello, Child Hello, Child true true
👍2❤1
CHALLENGE
class StringBuilder {
#parts = [];
append(s) {
this.#parts.push(s);
return this;
}
toString() {
return this.#parts.join('');
}
}
const sb = new StringBuilder().append('a').append('b').append('c');
console.log(`${sb}-${sb.append('d')}`);❤1👍1
❤2🔥2🤔2👍1
CHALLENGE
function curry(fn) {
return function curried(...args) {
if (args.length >= fn.length) return fn.apply(this, args);
return (...more) => curried.apply(this, [...args, ...more]);
};
}
function multiply(a, b, c = 1, ...rest) {
return a * b * c * (rest.reduce((s, n) => s + n, 0) || 1);
}
const curried = curry(multiply);
console.log(curried(2)(3), multiply.length);👍5❤2🔥1🤔1
❤1👍1
CHALLENGE
const target = { a: 1, b: 2 };
const handler = {
get(obj, prop, receiver) {
if (prop in obj) {
return Reflect.get(obj, prop, receiver);
}
return `missing:${String(prop)}`;
},
set(obj, prop, value, receiver) {
if (typeof value === 'number') {
return Reflect.set(obj, prop, value * 2, receiver);
}
return Reflect.set(obj, prop, value, receiver);
}
};
const p = new Proxy(target, handler);
p.c = 5;
p.d = 'hello';
console.log(p.a, p.c, p.d, p.z);❤3👍1🤔1
What is the output?
Anonymous Quiz
11%
1 5 hello missing:z
49%
1 10 hello missing:z
30%
1 10 hello undefined
10%
undefined 10 hello missing:z
❤1🤔1
CHALLENGE
const sym = Symbol('id');
const target = { a: 1, [sym]: 2 };
Reflect.defineProperty(target, 'b', { value: 3, enumerable: false });
const handler = {
get(obj, prop, receiver) {
if (prop === 'a') return Reflect.get(obj, prop, receiver) * 10;
return Reflect.get(obj, prop, receiver);
}
};
const proxy = new Proxy(target, handler);
console.log(proxy.a, proxy.b, Reflect.ownKeys(target).length, Object.keys(target).length);👍2🔥1
CHALLENGE
let a = 0;
let b = null;
let c = '';
let d;
a ||= 10;
b ??= 20;
c &&= 'changed';
d ??= a + b;
console.log(a, b, c, d);
❤2👍2🔥1
CHALLENGE
function build(a, b = a + 1, c = (() => a + b)()) {
a = 100;
return [a, b, c];
}
console.log(build(1), build(1, 2), build(5, 5, 5));CHALLENGE
let outerType;
{
function greet() {
return 'inner';
}
outerType = typeof greet;
}
console.log(outerType, typeof greet);
❤1