async handleSomething(data) {
const items = [];
for(let i = 0; i < data.length; i++) {
const item = { source: data[i] };
if (item.someCheck()) {
const things = await fetch('/api/things?type=1');
item.type = 'type-1';
this.$set(item, 'things', things);
} else {
item.type = 'type-2';
}
items.push(item);
}
this.$emit('get', items);
}
async handleSomething(data) {
const items = await Promise.all(data.map(async elem => {
const item = { source: elem };
if (item.someCheck()) {
const things = await fetch('/api/things?type=1');
item.type = 'type-1';
this.$set(item, 'things', things);
} else {
item.type = 'type-2';
}
return item;
});
this.$emit('get', items);
}
window.addEventListener("click", buildHandler('hello', bar));
function buildHandler(mes, cb) {
return function(e) {
cb(e, mes);
}
}
function bar(e, mes) {
console.log(e);
console.log(mes);
}
function Slider(props) {
/* ... */
this.images = props.images;
this.currentSlide = 1;
/* ... */
}
Slider.prototype.changeSlide = function() {
/* ... */
if (this.currentSlide === this.images.length) {
currentSlide = 1;
} else {
this.currentSlide++;
}
/* ... */
}
class Parent extends Component {
state = {
search: '',
data: [],
};
componentDidMount() {
fetchData().then(({ data }) => this.setState({ data }));
}
handleFilterChange = e => {
this.setState({ search: e.target.value });
};
render() {
const { data, search } = this.state;
const filteredData = data.filter(el => /* some stuff */);
return (
<Wrapper>
<SearchFilter onChange={this.handleFilterChange} />
<DataList data={filteredData} />
</Wrapper>
)
}
}
isValidEmail("mail@gmail.com").then(result => someAction(result));
async someHandler() {
const result = await isValidEmail("mail@gmail.com");
return someAction(result);
}
async someOtherHandler() {
const result = await isValidEmail("mail@gmail.com");
/* вызов someOtherAsyncAction тоже возвращает Promise, поэтому ждем
и возвращаем только когда придет результат, для этого используем
ключевое слово await */
return await someOtherAsyncAction(result);
}
class myClass {
createSomeObj() {
const newObj = { 1: 1, 2: 2 };
newObj.__proto__ = this.__proto__;
return newObj;
}
}
const instance = new myClass();
const instanceOfInstance = instance.createSomeObj();
console.log(instanceOfInstance.createSomeObj()); // myClass {1: 1, 2: 2}
class myClass {
createSomeObj() {
const newObj = { 1: 1, 2: 2 };
newObj.__proto__.constructor = myClass;
return newObj;
}
}
const instance = new myClass();
console.log(instance.createSomeObj()); // myClass {1: 1, 2: 2}
class myClass {
constructor(props) {
Object.keys(props).forEach(key => this[key] = props[key]);
}
static createSomeObj() {
return new myClass({ 1: 1, 2: 2 });
}
}
console.log(myClass.createSomeObj()); // myClass {1: 1, 2: 2}
itemsAmount = itemsAmount - itemCoast; // из общей суммы корзины вычитаем стоимость товара