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 #2859
4.09K
- 🔥 5
- ❤ 3
- 👍 3
JA @javascript
Showing posts older than #2860 · Back to latest
const x = null;
const y = undefined;
const z = 0;
const result1 = x ?? 'default';
const result2 = y ?? 'default';
const result3 = z ?? 'default';
const combined = result1 ? result2 ? result3 : 'B' : 'C';
console.log(combined);

function Vehicle(type) {
this.type = type;
}
Vehicle.prototype.wheels = 4;
Vehicle.prototype.getInfo = function() {
return `${this.type}: ${this.wheels}`;
};
const car = new Vehicle('Car');
const bike = Object.create(car);
bike.type = 'Bike';
bike.wheels = 2;
console.log(car.getInfo() + ' | ' + bike.getInfo());
const obj = { a: 1, b: 2, c: 3 };
Object.defineProperty(obj, 'd', {
value: 4,
enumerable: false
});
const entries = Object.entries(obj);
const keys = Object.keys(obj);
const values = Object.values(obj);
console.log(entries.length + keys.length + values.length);
class Subject {
constructor() {
this.observers = [];
}
attach(observer) {
this.observers.push(observer);
}
notify(data) {
this.observers.forEach(obs => obs.update(data));
}
}
const subject = new Subject();
subject.attach({ update: (d) => console.log(d * 2) });
subject.attach({ update: (d) => console.log(d + 5) });
subject.notify(10);const a = { x: 1 };
const b = a;
const c = { x: 1 };
b.x = 2;
const d = b;
d.x = 3;
console.log(a.x);
console.log(b.x);
console.log(c.x);
console.log(a === b);
console.log(a === c);const curry = (fn) => {
return function curried(...args) {
if (args.length >= fn.length) {
return fn.apply(this, args);
}
return (...nextArgs) => curried(...args, ...nextArgs);
};
};
const multiply = (a, b, c) => a * b * c;
const curriedMultiply = curry(multiply);
const step1 = curriedMultiply(2);
const step2 = step1(3);
const result = step2(4);
console.log(result);const x = 5;
const y = 10;
const obj = {
x,
y,
z: x + y,
calculate() {
return this.x * this.y;
},
[x + y]: 'computed'
};
console.log(obj.calculate() + obj[15] + obj.z);
const promise1 = Promise.resolve(10);
const promise2 = promise1.then(x => x * 2);
const promise3 = promise2.then(x => {
console.log(x);
return x + 5;
});
const promise4 = promise2.then(x => {
console.log(x);
return x * 3;
});
Promise.all([promise3, promise4]).then(results => {
console.log(results);
});