function dataPage() {
fetch('example.com')
.then(response => response.json())
.then(data => {
const promises = data.URLs.map(url => fetch(url).then(r => r.json()))
return Promise.all(promises).then((arr) => {
data.arr = arr;
return data;
})
})
.then((data) => {
setState(prevState => {
return (
{
...prevState,
data // вероятно тут спред нужен по логике
}
)
})
})
}
const root = document.getElementById('root');
function writeText(text, delay) {
if (!text) {
return;
}
const char = text[0];
const remaining = text.slice(1);
setTimeout(() => {
root.textContent = root.textContent + char;
writeText(remaining, delay);
}, delay);
}
function run() {
writeText('hello world', 50);
}
class Foo {
foo = 5;
bar = () => {
console.log(this.foo);
};
}
// вспомогательные функции вырезаны из ответа для упрощения
var Foo = function Foo() {
var _this = this;
_classCallCheck(this, Foo);
_defineProperty(this, "bar", function () {
console.log(_this.foo);
});
this.foo = 5;
};
// возвращается список, а не один элемент, поэтому работать не будет
document.getElementsByClassName('site').classList.add('container');
// вот так будет один элемент, если он есть в DOM-дереве
document.getElementsByClassName('site')[0].classList.add('container');
// вот так тоже будет один элемент, если он есть в DOM-дереве
document.querySelector('.site').classList.add('container');
function isValidEmail(value) {
const len = value.length
const idx = value.indexOf('@')
return len >= 3 && idx > 0 && idx < len - 1;
}
console.log(isValidEmail('contact@example.com')); // true
console.log(isValidEmail('a@b')); // true
console.log(isValidEmail('@a')); // false
console.log(isValidEmail('b@')); // false
console.clear();
function saveCalls(func) {
const calls = [];
function proxy(...args) {
var ctx = this;
calls.push({
ctx: ctx,
args: args,
});
return func.apply(ctx, args);
}
proxy.calls = calls;
return proxy;
}
function sum(a, b) {
return a + b;
}
const saveSum = saveCalls(sum);
console.log(saveSum(5, 10));
console.log(saveSum(3, 1));
console.log(saveSum.calls);
const user = {
name: 'john',
say() {
return this.name;
},
};
const saveSay = saveCalls(user.say);
console.log(saveSay.call(user));
console.log(saveSay.calls);
import React from "react";
import moment from "moment";
import 'moment/locale/ru'; // create-react-app specific
moment.locale("ru");
const target = moment("2020-08-17 23:00:00");
const toNow = target.toNow(true);
export default function App() {
return (
<div>
<p>Осталось {toNow}</p>
</div>
);
}
return { ...acc, [name]: arg }
var tmp = {};
tmp[name] = arg;
return Object.assing({}, acc, tmp);