CHALLENGE
function Vehicle(make, model, year) {
this.make = make;
this.model = model;
this.year = year;
this.describe = function () {
return `${this.year} ${this.make} ${this.model}`;
};
}
Vehicle.prototype.age = function (currentYear) {
return currentYear - this.year;
};
const car = new Vehicle("Toyota", "Supra", 1998);
const bike = new Vehicle("Harley", "Sportster", 2005);
console.log(car.describe());
console.log(bike.age(2025));
console.log(car.constructor === Vehicle);
console.log(Object.getPrototypeOf(car) === Vehicle.prototype);
👍2
What is the output?
Anonymous Quiz
14%
1998 Toyota Supra 27 false true
54%
1998 Toyota Supra 20 true true
21%
1998 Toyota Supra 20 true false
11%
Toyota Supra 1998 27 true true
❤3👍2🔥2
Please open Telegram to view this post
VIEW IN TELEGRAM
❤7🤔2
CHALLENGE
class EventEmitter {
#listeners = new WeakMap();
#registry = new FinalizationRegistry((label) => {
console.log(`Cleaned up: ${label}`);
});
subscribe(target, callback) {
if (!this.#listeners.has(target)) {
this.#listeners.set(target, []);
}
this.#listeners.get(target).push(callback);
this.#registry.register(target, target.name ?? "unknown");
}
emit(target) {
const cbs = this.#listeners.get(target);
if (cbs) cbs.forEach(cb => cb());
}
}
const emitter = new EventEmitter();
let obj1 = { name: "sensor" };
let obj2 = { name: "timer" };
const ws = new WeakSet([obj1, obj2]);
emitter.subscribe(obj1, () => console.log("sensor fired"));
emitter.subscribe(obj2, () => console.log("timer fired"));
emitter.subscribe(obj1, () => console.log("sensor logged"));
emitter.emit(obj1);
console.log(ws.has(obj1));
obj1 = null;
console.log(ws.has({ name: "sensor" }));🔥2❤1
The Node.js team has long been discussing shifting Node to a new schedule of one major release per year (instead of two), removing the odd/even distinction, and making every release LTS (with a prior 11 months of alpha/current status). This is a preview post not intended for final publication till April, so things are subject to change (backup version).
The Node.js Team
Please open Telegram to view this post
VIEW IN TELEGRAM
❤1