071-modern-js.html 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>071 modern JavaScript</title>
  6. <style>
  7. body { background: #ffffff; color: #202020; font-family: sans-serif;
  8. margin: 12px; font-size: 18px; }
  9. #list li { margin: 2px 0; }
  10. #result { background: #ffc8c8; color: #000000; padding: 8px;
  11. margin-top: 12px; }
  12. </style>
  13. </head>
  14. <body>
  15. <h1>071 &mdash; modern JavaScript</h1>
  16. <p>ES2015+ syntax and built-ins drive the DOM: classes, arrow functions,
  17. template literals, destructuring, spread, Map/Set, optional chaining,
  18. async/await with timers and Promise jobs.</p>
  19. <ul id="list"></ul>
  20. <div id="result">checking...</div>
  21. <script>
  22. 'use strict';
  23. const apps = [
  24. { id: 'a', title: 'Alpha', tags: ['chat'] },
  25. { id: 'b', title: 'Beta', tags: ['blog', 'chat'] },
  26. { id: 'c', title: 'Gamma' },
  27. ];
  28. class Store {
  29. #items;
  30. constructor(items) { this.#items = [...items]; }
  31. byTag(tag) { return this.#items.filter(a => a.tags?.includes(tag) ?? false); }
  32. get size() { return this.#items.length; }
  33. }
  34. const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
  35. async function main() {
  36. const store = new Store(apps);
  37. const list = document.getElementById('list');
  38. list.innerHTML = apps.map(({ id, title }) => `<li data-id="${id}">${title}</li>`).join('');
  39. const seen = new Set();
  40. for (const li of list.querySelectorAll('li')) seen.add(li.dataset.id);
  41. await sleep(50);
  42. const counts = new Map([['chat', store.byTag('chat').length]]);
  43. let clicked = '';
  44. list.addEventListener('click', e => { clicked = e.target.textContent; });
  45. list.querySelector('li[data-id="b"]').click();
  46. const { size } = store;
  47. const ok = seen.size === 3 && counts.get('chat') === 2 && size === 3 &&
  48. clicked === 'Beta' && Object.entries({ x: 1 }).flat().join('') === 'x1';
  49. const r = document.getElementById('result');
  50. r.style.background = ok ? '#c8f0c8' : '#ffc8c8';
  51. r.textContent = `${ok ? 'MODERN-OK' : 'MODERN-FAIL'} seen=${seen.size} chat=${counts.get('chat')} size=${size} clicked=${clicked}`;
  52. }
  53. main().catch(e => { document.getElementById('result').textContent = 'MODERN-FAIL ' + e; });
  54. </script>
  55. </body>
  56. </html>