A resourceful newsletter featuring the latest and most important news, articles, books and updates in the world of #javascript 🚀 Don't miss our Quizzes!
Let's chat: @nairihar
Post #2536
5.38K
JA @javascript
Showing posts older than #2537 · Back to latest

class Counter {
constructor(max) {
this.max = max;
}
*[Symbol.iterator]() {
let current = 0;
while (current < this.max) {
yield current++;
}
}
}
const counter = new Counter(3);
const result = [...counter, ...counter];
console.log(result);
const target = { name: 'Sarah', age: 25 };
const handler = {
get(obj, prop) {
if (prop in obj) {
return obj[prop];
}
return `Property '${prop}' not found`;
},
set(obj, prop, value) {
obj[prop] = value.toString().toUpperCase();
return true;
}
};
const proxy = new Proxy(target, handler);
proxy.city = 'boston';
console.log(proxy.name);
console.log(proxy.city);
console.log(proxy.country);class Logger {
constructor(prefix) {
this.prefix = prefix;
}
log(message) {
console.log(`${this.prefix}: ${message}`);
}
}
class Database {
constructor(logger) {
this.logger = logger;
}
save(data) {
this.logger.log(`Saving ${data}`);
return `${data}_saved`;
}
}
const logger = new Logger('DB');
const db = new Database(logger);
const result = db.save('user');
console.log(result);const Flyable = {
fly() { return `${this.name} is flying`; }
};
const Swimmable = {
swim() { return `${this.name} is swimming`; }
};
function createDuck(name) {
return Object.assign({ name }, Flyable, Swimmable);
}
const duck = createDuck('Quackers');
console.log(duck.fly());
console.log(duck.swim());
console.log(Object.getOwnPropertyNames(duck));const compose = (...fns) => x => fns.reduceRight((acc, fn) => fn(acc), x);
const addPrefix = str => `prefix_${str}`;
const capitalize = str => str.charAt(0).toUpperCase() + str.slice(1);
const addSuffix = str => `${str}_suffix`;
const process = compose(addSuffix, capitalize, addPrefix);
console.log(process('data'));

class VideoCall {
constructor() {
this.pc = { iceConnectionState: 'new' };
this.streams = [];
}
async connect() {
this.pc.iceConnectionState = 'checking';
await Promise.resolve();
this.pc.iceConnectionState = 'connected';
this.streams.push('remote-video');
return this.pc.iceConnectionState;
}
handleConnectionChange() {
const states = ['new', 'checking', 'connected'];
return states.map(state => {
return this.pc.iceConnectionState === state ? `Status: ${state}` : null;
}).filter(Boolean);
}
}
const call = new VideoCall();
call.connect().then(() => {
console.log(call.handleConnectionChange());
});class ChatServer {
constructor() {
this.clients = new Set();
this.messageLog = [];
}
connect(client) {
this.clients.add(client);
return () => this.clients.delete(client);
}
broadcast(message, sender) {
this.messageLog.push(message);
this.clients.forEach(client => {
if (client !== sender) {
client.receive(message);
}
});
}
}
const server = new ChatServer();
const david = { name: 'David', receive: msg => console.log(`David got: ${msg}`) };
const sarah = { name: 'Sarah', receive: msg => console.log(`Sarah got: ${msg}`) };
const emma = { name: 'Emma', receive: msg => console.log(`Emma got: ${msg}`) };
const disconnectDavid = server.connect(david);
server.connect(sarah);
server.connect(emma);
server.broadcast('Hello everyone!', david);
disconnectDavid();
server.broadcast('Is David still here?', sarah);
console.log(server.clients.size);