let one = ['a', 'b', 'c'];
let two = ['1', '2', '3'];
const inp = '2';
const foundList = [one, two].find(list => list.includes(inp));
switch (foundList) {
case one: console.log('one'); break;
case two: console.log('two'); break;
}
var goods = {};
const numberOfGoods = 100;
for (let i = 0; i < numberOfGoods; i++) {
goods[`item${i}`] = {
id: i,
name: `диван №${i}`,
description: `тут описание дивана №${i}`,
price: Math.round(Math.random() * 200 + 1000), // цены рандомно от 1000 до 1200
}
}
var out ='';
for (var key in goods){
out += 'Название: ' + goods[key].name+ '<br/>';
out += 'Описание: ' + goods[key].description+ '<br/>';
out += 'Есть на складе: ' + goods[key].price+ '<br/>'; // почему вдруг "pirce" = "есть на складе" ¯\_ツ_/¯
out += '<hr>';
}
document.getElementById('outGoods').innerHTML = out;
Как начать разрабатывать игры или написать свой игровой движок?
interface Some {
value: string | string[];
}
type Extends<E, T extends E> = T;
const foo = {
value: ['a', 'b'] // здесь всегда будет массив
}
foo as Extends<Some, typeof foo>;
foo as Some
не отловит все, что нужно, например {value: ['a', 'b', 8]}
let $body = document.querySelector('body');
let $bodyH = $body.clientHeight;
let $bodyW = $body.clientWidth;
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function main() {
let ask = prompt('How many circles you want to draw?');
if (ask !== null || ask !== '') {
for (let i = 0; i < ask; i++) {
drowCircle(i);
await sleep(1000);
}
}
}
main();
function drowCircle(i) {
let $circle = document.createElement('div');
let $circleSize = getRand(50, 100);
let positionX = getRand(0, $bodyW - $circleSize);
let positionY = getRand(0, $bodyH - $circleSize);
$body.insertAdjacentElement('afterbegin', $circle);
$circle.style.width = $circle.style.height = `${$circleSize}px`;
$circle.style.zIndex = i+1;
$circle.style.backgroundColor = `rgb(${getRand(0,255)},${getRand(0,255)},${getRand(0,255)})`;
$circle.style.borderRadius = '50%';
$circle.style.position = 'absolute';
$circle.style.top = `${positionY}px`;
$circle.style.left = `${positionX}px`;
}
function getRand(min, max) {
let rand = min - 0.5 + Math.random() * (max - min + 1);
return Math.round(rand);
}
async function runAgents<T extends IAgentList>(agents: T): Promise<ResolveResult<T>>
type ResolveResult<T extends IAgentList> = {
[key in keyof T]: T[key] extends IAgentBase<infer R> ? R : never;
}
type IItem = {type: 'IItem'};
type IDataSet = {type: 'IDataSet'};
export type IAgentList = {
[key: string]: IAgentBase<any>;
}
export interface IAgents extends IAgentList {
items: IItemsParser;
page: IPageParser;
parser: IWebParser;
}
export interface IAgentBase<T> {
parse(): Promise<T>;
}
export interface IItemsParser extends IAgentBase<IItem[]> {
}
export interface IPageParser extends IAgentBase<IDataSet[]> {
}
export interface IWebParser extends IAgentBase<any> {
}
export interface IAgentsResult<T> {
items?: IItem[];
page?: IDataSet[];
parser?: any;
}
type ResolveResult<T extends IAgentList> = {
[key in keyof T]: T[key] extends IAgentBase<infer R> ? R : never;
}
// в реальности все иначе, написал для понимания желаемого результата
async function runAgents<T extends IAgentList>(agents: T): Promise<ResolveResult<T>> {
const result: ResolveResult<T> = {} as any;
Object.keys(agents).forEach(
async key => result[key as keyof T] = await agents[key as keyof T].parse()
);
return result;
}
async function main() {
const agents : IAgents = {} as any;
const result = await runAgents(agents)
result // typed
}
xhr.open('GET','inner.js',true)