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 #3407
2.55K
- ❤ 4
JA @javascript
Showing posts older than #3408 · Back to latest
const order = [];
const log = (s) => order.push(s);
log('start');
Promise.resolve().then(() => {
log('p1');
return Promise.resolve();
}).then(() => log('p1-2'));
async function foo() {
log('foo-start');
await null;
log('foo-end');
}
foo();
Promise.resolve().then(() => log('p2'));
log('end');
setTimeout(() => console.log(order.join(' ')), 0);
function tag(strings, ...values) {
return strings.raw.join('|') + '=' + values.reduce((sum, v) => sum + v, 0);
}
const x = 5;
const y = 10;
console.log(tag`Sum\n${x}and${y}end`);const config = { retries: 0, timeout: null, name: '' };
const a = config.retries || 5;
const b = config.timeout ?? 10;
const c = config.name || 'default';
const d = config.missing?.value ?? 'fallback';
let calls = 0;
const sideEffect = () => { calls++; return true; };
const e = config.retries && sideEffect();
console.log(a, b, c, d, e, calls);function tag(strings, ...values) {
// strings.raw preserves literal escape sequences
const rawParts = strings.raw;
let result = rawParts.join('|');
result += '::' + values.join(',');
return result;
}
const x = 5;
const output = tag`Line1\nLine2${x}End`;
console.log(output);
function* inner() {
yield 1;
yield 2;
return 10;
}
function* outer() {
const result = yield* inner();
yield result;
yield 3;
}
const gen = outer();
const a = gen.next().value;
const b = gen.next().value;
const c = gen.next().value;
const d = gen.next().value;
console.log(a, b, c, d);
class Base {
static count = 0;
static #secret;
static {
Base.#secret = 10;
Base.count += 1;
}
static getSecret() {
return Base.#secret;
}
}
class Derived extends Base {
static {
Base.count += 100;
}
}
console.log(Base.count, Derived.count, Base.getSecret());function Animal(name) {
this.name = name;
}
Animal.prototype.speak = function () {
return `${this.name} makes a sound`;
};
function Dog(name) {
Animal.call(this, name);
}
Dog.prototype = Object.create(Animal.prototype);
Dog.prototype.constructor = Dog;
Dog.prototype.speak = function () {
return `${Animal.prototype.speak.call(this)} (bark)`;
};
const d = new Dog('Rex');
console.log(
d.speak(),
d instanceof Animal,
d.constructor === Dog,
Object.getPrototypeOf(d) === Dog.prototype
);
export {};const source = {
_val: 1,
get val() { return this._val; }
};
const copy1 = Object.assign({}, source);
source._val = 100;
const copy2 = { ...source };
copy1._val = 999;
console.log(copy1.val, copy2.val, source.val, copy1._val);const arr = [1, [2, 3], [4, [5, 6]], 7];
const a = arr.flat();
const b = arr.flat(2);
const c = arr.flatMap(x => Array.isArray(x) ? x : [x, x]);
const d = [1, 2, 3].flatMap(x => [[x]]);
console.log(JSON.stringify([a, b, c, d]));