CHALLENGE
class CustomError extends Error {
constructor(message) {
super(message);
this.name = 'CustomError';
}
}
try {
throw new CustomError('Something went wrong');
} catch (e) {
console.log(e instanceof Error);
console.log(e instanceof CustomError);
console.log(e.constructor.name);
console.log(e.name);
}What is the output?
Anonymous Quiz
24%
true, false, Error, CustomError
32%
false, true, CustomError, Error
41%
true, true, CustomError, CustomError
4%
true, true, Error, Error
👍5❤3🔥2
CHALLENGE
class EventEmitter {
constructor() {
this.events = {};
}
on(event, callback) {
this.events[event] = this.events[event] || [];
this.events[event].push(callback);
return this;
}
emit(event, data) {
if (this.events[event]) {
this.events[event].forEach(cb => cb(data));
}
return this;
}
}
class Logger {
log(msg) { console.log(`[LOG]: ${msg}`); }
}
class DataProcessor {
constructor(emitter, logger) {
this.emitter = emitter;
this.logger = logger;
this.emitter.on('process', (data) => {
this.logger.log(data.toUpperCase());
});
}
process(data) {
this.emitter.emit('process', data);
}
}
const emitter = new EventEmitter();
const logger = new Logger();
const processor = new DataProcessor(emitter, logger);
processor.process('hello world');
emitter.emit('process', 'composition rocks');👍3❤1
Did you know the Node.js project maintains a page about security best practices organized around how to mitigate ten of the most significant vectors? Topics include networking weaknesses, timing attacks, supply chain attacks, and the monkey patching of intrinsics.
Node Documentation
Please open Telegram to view this post
VIEW IN TELEGRAM
❤3🔥1
CHALLENGE
const obj = { count: 0 };
const arr = [obj, obj, obj];
function increment(item) {
item.count++;
return item;
}
const results = arr.map(increment);
console.log(obj.count);
console.log(results[0] === results[1]);
console.log(results.length);
console.log(arr[0].count);❤4
❤3👍3🤔2