const fizz = {...bazz, x: 1};
const {x, ...fizz} = bazz;
Q: В чем разница между этими двумя выражениями?
Простой вопрос на понимание spred-операторов, но иногда заставляет людей призадуматься
const obj1 = {
id: 1,
data: {
userId: 1,
userName: 'Test',
userTags: ['admin', 'vip'],
}
Q: Я хочу получить копию объекта obj1, которая была бы полностью независима от оригинала
Можно также расспросить чем будет отличаться поведение при этих способах копирования и почему
const objCopy1 = Object.assign({}, obj1); const objCopy2 = {...obj1}; const objCopy3 = JSON.parse(JSON.stringify(obj1));
const obj = {};
obj.a = 1;
obj.test = function () {
setTimeout(function() {
console.log(this.a);
})
};
obj.test();
________________________________________________________________________
function test() { console.log(this); };
setTimeout(function () { test(); }, 100);
setTimeout(() => { test(); }, 100);
setTimeout(test, 100);
A: global, global, timeout context
________________________________________________________________________
async function sleep(time: number) { return new Promise((resolve) => setTimeout(resolve, time)); }
async function test1() { console.log('a1');
await sleep(100); console.log('b1');
await sleep(100); console.log('c1'); }
async function test2() { console.log('a2');
await sleep(100); console.log('b2');
await sleep(100); console.log('c2'); }
Promise.all([test1(), test2()]);
Q: _Что будет выводить этот код? Как измениться его поведение, если убрать все `await`-ы и почему?_
На вопрос все в основном отвечают правильно, но объяснение помогает мне понять, насколько хорошо канди дат представляет себе внутренее устройство event loop-а
________________________________________________________________________
class Parent { constructor(params) { Object.assign(this, params); }
static fabric() { return new Parent({ source: 'fabric', test: 1 }); }
title() { return "I'm Parent"; } }
class Children extends Parent { title() { return "I'm Children"; } }
const parent = Parent.fabric(); const children = Children.fabric();
console.log(parent.title(), children.title());
Q: _Как нужно изменить метод `fabric` в классе Parent, чтобы Children.fabric() (и все другие наследники класса Parent) возвращал экземпляр своего класса?_
________________________________________________________________________
const obj1 = { a: 1, b: 2, c: 3 };
const obj2 = { a: 10, b: null, c: undefined, d: null, e: undefined, };
console.log({ ...obj1, ...obj2 });
________________________________________________________________________
https://codesandbox.io/s/hopeful-violet-5lhj5
https://codesandbox.io/s/gracious-euler-fe5o3?file=/src/index.js
_______________________________________________________________________
setTimeout(function timeout() { console.log('timeout'); }, 0); let p = new Promise(function(resolve, reject) { console.log('create promise'); resolve(); }); p.then(function() { console.log('resolve promise'); }); console.log('end script');