<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Javascript on Osmar Petry</title><link>https://osmarpetry.dev/tags/javascript/</link><description>Recent content in Javascript on Osmar Petry</description><generator>Hugo</generator><language>en-US</language><lastBuildDate>Mon, 23 Mar 2026 00:00:00 +0000</lastBuildDate><atom:link href="https://osmarpetry.dev/tags/javascript/rss.xml" rel="self" type="application/rss+xml"/><item><title>JavaScript Modules and Bundlers</title><link>https://osmarpetry.dev/blog/javascript-modules-and-bundlers/</link><pubDate>Mon, 23 Mar 2026 00:00:00 +0000</pubDate><guid>https://osmarpetry.dev/blog/javascript-modules-and-bundlers/</guid><description>&lt;p&gt;The goal here is to understand JavaScript module systems (IIFE, CommonJS, ESModules), how modern bundlers sit on top of them, and which build types to use for business apps and component libraries.&lt;/p&gt;&#10;&lt;p&gt;Module systems describe &lt;strong&gt;how code is organized and imported/exported&lt;/strong&gt; at the language level. Bundlers and build tools &lt;strong&gt;consume those modules&lt;/strong&gt; and produce optimized outputs for different environments (browser, Node, CDN, etc.).&lt;/p&gt;&#10;&lt;hr&gt;&#10;&lt;h2 id="part-1--module-systems"&gt;Part 1 — Module Systems&lt;/h2&gt;&#10;&lt;h3 id="iife-immediately-invoked-function-expression"&gt;IIFE (Immediately Invoked Function Expression)&lt;/h3&gt;&#10;&lt;p&gt;IIFEs are the classic pattern to create a private scope and avoid polluting &lt;code&gt;window&lt;/code&gt;, often combined with a single global namespace like &lt;code&gt;App&lt;/code&gt;.&lt;/p&gt;</description></item><item><title>Web Accessibility V3 — Topics cheat-sheet &amp; decision guide</title><link>https://osmarpetry.dev/blog/web-accessibility/</link><pubDate>Mon, 01 Sep 2025 00:00:00 +0000</pubDate><guid>https://osmarpetry.dev/blog/web-accessibility/</guid><description>&lt;h2 id="summary"&gt;Summary&lt;/h2&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;Course map covers &lt;strong&gt;Screen Readers&lt;/strong&gt;, &lt;strong&gt;Accessible HTML&lt;/strong&gt;, &lt;strong&gt;ARIA&lt;/strong&gt;, &lt;strong&gt;Focus Management&lt;/strong&gt;, and &lt;strong&gt;Visual Considerations&lt;/strong&gt;, plus &lt;strong&gt;Debugging &amp;amp; team process&lt;/strong&gt;.&lt;/li&gt;&#10;&lt;li&gt;Aim: ship &lt;strong&gt;perceivable, operable, understandable, robust&lt;/strong&gt; experiences (POUR) with practical exercises and team-ready debugging habits.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;hr&gt;&#10;&lt;h2 id="topics--what-to-do-at-a-glance"&gt;Topics → what to do (at a glance)&lt;/h2&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;strong&gt;Screen Readers:&lt;/strong&gt; learn how they work, enable them, write solid &lt;strong&gt;alt text&lt;/strong&gt;, and test.&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;Accessible HTML:&lt;/strong&gt; prefer &lt;strong&gt;semantic elements&lt;/strong&gt; and set &lt;strong&gt;document language&lt;/strong&gt;; fix invalid markup.&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;ARIA:&lt;/strong&gt; add only when semantics aren’t enough; know &lt;strong&gt;roles, states, properties&lt;/strong&gt;, &lt;strong&gt;names/descriptions&lt;/strong&gt;, and &lt;strong&gt;live regions&lt;/strong&gt;.&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;Focus Management:&lt;/strong&gt; full keyboard support, &lt;strong&gt;skip links&lt;/strong&gt;, logical &lt;strong&gt;Tab&lt;/strong&gt; order, avoid &lt;strong&gt;focus traps&lt;/strong&gt;, and review shortcuts.&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;Visual:&lt;/strong&gt; check &lt;strong&gt;color contrast&lt;/strong&gt;, &lt;strong&gt;reflow/zoom&lt;/strong&gt;, &lt;strong&gt;reduced motion&lt;/strong&gt;, and &lt;strong&gt;prefers-color-scheme&lt;/strong&gt;; connect performance with accessibility.&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;Debugging:&lt;/strong&gt; use linters/devtools, improve team workflows, go &lt;strong&gt;beyond compliance&lt;/strong&gt;, and practice with hands-on exercises.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;hr&gt;&#10;&lt;h2 id="decision-heuristics"&gt;Decision heuristics&lt;/h2&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;strong&gt;Semantics first → ARIA last.&lt;/strong&gt; If a native element already communicates role/state, &lt;strong&gt;don’t add ARIA&lt;/strong&gt;.&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;Keyboard is the truth.&lt;/strong&gt; If it’s not operable with keyboard and visible focus, it’s not accessible.&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;Name, role, value:&lt;/strong&gt; can a screen reader expose them correctly? If not, fix semantics, labels, or ARIA.&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;Respect user prefs:&lt;/strong&gt; &lt;code&gt;prefers-reduced-motion&lt;/code&gt;, &lt;code&gt;prefers-color-scheme&lt;/code&gt;, and zoom/reflow without loss.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;hr&gt;&#10;&lt;h2 id="team-workflow"&gt;Team workflow&lt;/h2&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;&lt;strong&gt;Set standards&lt;/strong&gt; (WCAG targets, browser/AT matrix).&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;Lint &amp;amp; test early&lt;/strong&gt; (HTML validity, axe, devtools).&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;Exercise mindset:&lt;/strong&gt; practice with screen readers and keyboard on real UI (modals, forms, navigation).&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;Go beyond checklists:&lt;/strong&gt; optimize usability for assistive technology users, not just box-ticking.&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;hr&gt;&#10;&lt;h2 id="dev-checklist"&gt;Dev checklist&lt;/h2&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;input disabled="" type="checkbox"&gt; Landmark structure (&lt;code&gt;header&lt;/code&gt;, &lt;code&gt;nav&lt;/code&gt;, &lt;code&gt;main&lt;/code&gt;, &lt;code&gt;aside&lt;/code&gt;, &lt;code&gt;footer&lt;/code&gt;).&lt;/li&gt;&#10;&lt;li&gt;&lt;input disabled="" type="checkbox"&gt; Page language (&lt;code&gt;&amp;lt;html lang&amp;gt;&lt;/code&gt;).&lt;/li&gt;&#10;&lt;li&gt;&lt;input disabled="" type="checkbox"&gt; Forms: &lt;code&gt;&amp;lt;label&amp;gt;&lt;/code&gt; + programmatic names; error text announced.&lt;/li&gt;&#10;&lt;li&gt;&lt;input disabled="" type="checkbox"&gt; Interactive controls are &lt;strong&gt;native&lt;/strong&gt; or fully re-created (role, keyboard, focus, ARIA).&lt;/li&gt;&#10;&lt;li&gt;&lt;input disabled="" type="checkbox"&gt; Focus order and &lt;strong&gt;visible&lt;/strong&gt; focus style.&lt;/li&gt;&#10;&lt;li&gt;&lt;input disabled="" type="checkbox"&gt; Contrast meets WCAG; no information by color alone.&lt;/li&gt;&#10;&lt;li&gt;&lt;input disabled="" type="checkbox"&gt; Motion: provide a &lt;strong&gt;reduced motion&lt;/strong&gt; path; avoid parallax autoplay by default.&lt;/li&gt;&#10;&lt;li&gt;&lt;input disabled="" type="checkbox"&gt; Respect zoom/reflow at 200–400% without horizontal scroll.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;hr&gt;&#10;&lt;h2 id="references"&gt;References&lt;/h2&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;strong&gt;Web Accessibility — All Topics (Frontend Masters companion site)&lt;/strong&gt;: &lt;a href="https://web-accessibility-v3.vercel.app/topics" target="_blank" rel="noreferrer"&gt;https://web-accessibility-v3.vercel.app/topics&lt;/a&gt;&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;GitHub repo&lt;/strong&gt;: &lt;a href="https://github.com/marcysutton/frontend-masters-web-accessibility-v3" target="_blank" rel="noreferrer"&gt;https://github.com/marcysutton/frontend-masters-web-accessibility-v3&lt;/a&gt;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;h2 id="related-notes"&gt;Related Notes&lt;/h2&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;[[react-forwardRef|Forwarding Ref]]&lt;/li&gt;&#10;&lt;li&gt;[[svg-are-the-best|SVG are the Best]]&lt;/li&gt;&#10;&lt;/ul&gt;</description></item><item><title>XState — model UI behavior correctly and make it testable</title><link>https://osmarpetry.dev/blog/xstate-model-ui-bahavior/</link><pubDate>Sat, 12 Jul 2025 00:00:00 +0000</pubDate><guid>https://osmarpetry.dev/blog/xstate-model-ui-bahavior/</guid><description>&lt;h2 id="the-problem-were-solving"&gt;The problem we&amp;rsquo;re solving&lt;/h2&gt;&#10;&lt;p&gt;Imagine you join a company that runs an e-commerce platform. Product asks you to build a &lt;strong&gt;search page&lt;/strong&gt; — the kind of page every user hits dozens of times a day. It sounds simple: a text input, a button, show results.&lt;/p&gt;&#10;&lt;p&gt;But then reality kicks in. The page has to handle empty queries, network failures, slow connections, retries, and race conditions where a user submits a second search before the first one finishes. A junior developer might reach for a handful of &lt;code&gt;useState&lt;/code&gt; calls — &lt;code&gt;isLoading&lt;/code&gt;, &lt;code&gt;hasError&lt;/code&gt;, &lt;code&gt;data&lt;/code&gt;, &lt;code&gt;submitted&lt;/code&gt; — and suddenly you&amp;rsquo;re juggling four booleans that produce sixteen possible combinations. Most of those combinations are impossible, but nothing in the code prevents them. A loading spinner stays visible after an error. A success message flashes before the data arrives. Bugs creep in not because the developer made a logic error, but because the &lt;strong&gt;model itself was never defined&lt;/strong&gt;.&lt;/p&gt;</description></item><item><title>Teach Yourself Programming in Ten Years</title><link>https://osmarpetry.dev/blog/teach-yourself-programming-ten-years-2001/</link><pubDate>Tue, 15 Apr 2025 00:00:00 +0000</pubDate><guid>https://osmarpetry.dev/blog/teach-yourself-programming-ten-years-2001/</guid><description>&lt;h2 id="abstract"&gt;Abstract&lt;/h2&gt;&#10;&lt;p&gt;Peter Norvig critiques promises of learning programming in weeks. Drawing on research in chess, music, and sports, he argues true mastery requires about ten years of deliberate practice—progressive challenges, feedback, and continual improvement. There are no shortcuts: success comes from sustained effort and critical self-analysis.&lt;/p&gt;&#10;&lt;h2 id="key-points"&gt;Key points&lt;/h2&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;Questions the abundance of “Learn X in 24 Hours/21 Days” books.&lt;/li&gt;&#10;&lt;li&gt;Excellence in any complex field typically demands ~10 years of deliberate practice (citing K. Anders Ericsson’s work).&lt;/li&gt;&#10;&lt;li&gt;Historical examples (Mozart, Beatles) show their peak came only after years of intense practice.&lt;/li&gt;&#10;&lt;li&gt;No magical formulas; commitment to long-term learning is essential.&lt;/li&gt;&#10;&lt;li&gt;Practical advice: explore multiple languages, seek challenges, learn from peers, blend theory with real projects.&lt;/li&gt;&#10;&lt;li&gt;Books alone are insufficient; hands-on experience is irreplaceable.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;h2 id="connections"&gt;Connections&lt;/h2&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;Ties to deliberate practice theory and Malcolm Gladwell’s “10,000 hours” popularisation.&lt;/li&gt;&#10;&lt;li&gt;Informs discussions on computing education and depth vs superficial knowledge.&lt;/li&gt;&#10;&lt;li&gt;Supports lifelong learning and career development perspectives in tech.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;h2 id="questions"&gt;Questions&lt;/h2&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;How to balance theory and deliberate practice?&lt;/li&gt;&#10;&lt;li&gt;Does the “ten-year” model apply uniformly across computing domains?&lt;/li&gt;&#10;&lt;li&gt;How to maintain motivation through multi-year learning journeys?&lt;/li&gt;&#10;&lt;li&gt;Which strategies accelerate progress without sacrificing depth?&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;h2 id="personal-reflections"&gt;Personal reflections&lt;/h2&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;Reinforces patience and persistence when building programming expertise.&lt;/li&gt;&#10;&lt;li&gt;Relevant to today’s culture of immediacy—reminds us depth takes time.&lt;/li&gt;&#10;&lt;li&gt;Agrees that combining study, deliberate practice, and real projects yields steady growth.&lt;/li&gt;&#10;&lt;li&gt;Encourages valuing the learning process rather than chasing quick wins.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;h2 id="detailed-notes"&gt;Detailed notes&lt;/h2&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;Abundance of quick-learning resources doesn’t reflect real mastery requirements.&lt;/li&gt;&#10;&lt;li&gt;Deliberate practice: constant challenge, error analysis, improvement.&lt;/li&gt;&#10;&lt;li&gt;Role of mentors, community, and diverse language exposure.&lt;/li&gt;&#10;&lt;li&gt;Suggested languages: Python, Scheme, JavaScript, Alice, Squeak, Blockly (depending on learner profile).&lt;/li&gt;&#10;&lt;li&gt;“Ten years” is illustrative; the key is sustained, deliberate work.&lt;/li&gt;&#10;&lt;li&gt;Original essay: &lt;a href="https://norvig.com/21-days.html" target="_blank" rel="noreferrer"&gt;https://norvig.com/21-days.html&lt;/a&gt;&lt;/li&gt;&#10;&lt;/ul&gt;</description></item><item><title>JavaScript Sets, Maps, and Hashing</title><link>https://osmarpetry.dev/blog/javascript-set-map-hash/</link><pubDate>Sun, 09 Feb 2025 00:00:00 +0000</pubDate><guid>https://osmarpetry.dev/blog/javascript-set-map-hash/</guid><description>&lt;h2 id="tldr"&gt;TL;DR&lt;/h2&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;Notes captured on 2025-02-09 during Deveficiente algorithms study.&lt;/li&gt;&#10;&lt;li&gt;Set = unique values with O(1) add/remove; Map = key-value store with O(1) lookups.&lt;/li&gt;&#10;&lt;li&gt;Hashing is reference-based for objects; custom equality needs manual hashing/serialization.&lt;/li&gt;&#10;&lt;li&gt;Useful for caching and deduplication; V8 resolves collisions with internal linked lists/trees.&lt;/li&gt;&#10;&lt;li&gt;C# exposes &lt;code&gt;GetHashCode&lt;/code&gt;; in JS you must implement equivalent behaviour explicitly.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;h2 id="sets"&gt;Sets&lt;/h2&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;Store unique values; duplicates are ignored.&lt;/li&gt;&#10;&lt;li&gt;Backed by hash tables in engines like V8.&lt;/li&gt;&#10;&lt;li&gt;Typical operations (&lt;code&gt;add&lt;/code&gt;, &lt;code&gt;delete&lt;/code&gt;, &lt;code&gt;has&lt;/code&gt;) run in O(1) on average.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-js" data-lang="js"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kr"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;set&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Set&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nx"&gt;set&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;value1&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nx"&gt;set&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;value2&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;set&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;has&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;value1&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// true&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;set&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;size&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 2&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id="maps"&gt;Maps&lt;/h2&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;Store key-value pairs; keys can be any data type.&lt;/li&gt;&#10;&lt;li&gt;Also implemented with hashing for O(1) lookups.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-js" data-lang="js"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kr"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;map&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Map&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kr"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;object&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// &amp;#39;object&amp;#39;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id="hashing-behaviour"&gt;Hashing behaviour&lt;/h2&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;Objects/arrays are compared by reference. Two objects with identical structure are not equal unless they share the same reference.&lt;/li&gt;&#10;&lt;li&gt;For custom hash keys (similar to C# &lt;code&gt;GetHashCode&lt;/code&gt;), stringify values or build hashing utilities.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-js" data-lang="js"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;hashObject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kr"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;cache&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Map&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kr"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;test&amp;#34;&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nx"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;hashObject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;cached&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id="cache-patterns"&gt;Cache patterns&lt;/h2&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-js" data-lang="js"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kr"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;cache&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Map&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;getData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;has&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;}&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="kr"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;fetchFromSource&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nx"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id="internal-details-v8"&gt;Internal details (V8)&lt;/h2&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;Hash collisions handled via linked lists or adaptive structures (e.g., small arrays, balanced trees).&lt;/li&gt;&#10;&lt;li&gt;Sparse arrays may convert to hash tables internally for memory efficiency.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;h2 id="set-vs-map-recap"&gt;Set vs Map recap&lt;/h2&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;strong&gt;Set&lt;/strong&gt;: unique values.&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;Map&lt;/strong&gt;: key-value pairs, versatile for caching, memoization.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;h2 id="references"&gt;References&lt;/h2&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;MDN: &lt;a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Set" target="_blank" rel="noreferrer"&gt;Set&lt;/a&gt;, &lt;a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Map" target="_blank" rel="noreferrer"&gt;Map&lt;/a&gt;.&lt;/li&gt;&#10;&lt;li&gt;V8 blog on hash tables and inline caching.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;h2 id="related-notes"&gt;Related Notes&lt;/h2&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;[[javascript-closures-and-curries|Closures and Curries]]&lt;/li&gt;&#10;&lt;li&gt;[[react-forwardRef|Forwarding Ref]]&lt;/li&gt;&#10;&lt;/ul&gt;</description></item><item><title>Closures and Curries</title><link>https://osmarpetry.dev/blog/javascript-closures-and-curries/</link><pubDate>Wed, 15 Jan 2025 00:00:00 +0000</pubDate><guid>https://osmarpetry.dev/blog/javascript-closures-and-curries/</guid><description>&lt;p&gt;Closures and currying are both important concepts in JavaScript, especially for functional programming.&lt;/p&gt;&#10;&lt;h2 id="closure"&gt;Closure&lt;/h2&gt;&#10;&lt;p&gt;A closure is a function that has access to its outer scope even after the outer function has returned. It &amp;ldquo;closes over&amp;rdquo; its outer scope, hence the name. Here&amp;rsquo;s an example:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-javascript" data-lang="javascript"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;outerFunction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;innerFunction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;};&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kr"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;add5&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;outerFunction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;add5&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// 8&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;In the above example, &lt;code&gt;**innerFunction**&lt;/code&gt; has access to &lt;code&gt;**x**&lt;/code&gt; even after &lt;code&gt;**outerFunction**&lt;/code&gt; has returned. This allows us to keep state across multiple invocations of the inner function.&#10;We can use closures to store a function with a first parameter, you can see an example here:&#10;&lt;a href="https://paper-attachments.dropbox.com/s_7A088D71FB9B829C04A17241E13BE66493C6164E2DA798BD85C153CC4FEC5AE3_1566590561542_Screenshot&amp;#43;from&amp;#43;2019-08-23&amp;#43;17-02-30.png" target="_blank" rel="noreferrer"&gt;&lt;img src="https://paper-attachments.dropbox.com/s_7A088D71FB9B829C04A17241E13BE66493C6164E2DA798BD85C153CC4FEC5AE3_1566590561542_Screenshot+from+2019-08-23+17-02-30.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;</description></item><item><title>Prototype</title><link>https://osmarpetry.dev/blog/javascript-prototype/</link><pubDate>Wed, 15 Jan 2025 00:00:00 +0000</pubDate><guid>https://osmarpetry.dev/blog/javascript-prototype/</guid><description>&lt;p&gt;Every object in JavaScript has an internal link to another object called its &lt;strong&gt;prototype&lt;/strong&gt;. When you access a property that doesn&amp;rsquo;t exist on an object, the engine walks up this chain — prototype by prototype — until it finds the property or reaches &lt;code&gt;null&lt;/code&gt;. This is the &lt;strong&gt;prototype chain&lt;/strong&gt;, and it is the mechanism behind all inheritance in JavaScript.&lt;/p&gt;&#10;&lt;p&gt;Understanding prototypes is essential because &lt;code&gt;class&lt;/code&gt; syntax in ES6 is just syntactic sugar over this system. Under the hood, &lt;code&gt;extends&lt;/code&gt; sets up the same prototype chain. Knowing the raw model helps debug unexpected property lookups, &lt;code&gt;instanceof&lt;/code&gt; checks, and performance issues around deep prototype chains.&lt;/p&gt;</description></item><item><title>React ForwardRef</title><link>https://osmarpetry.dev/blog/react-forwardref/</link><pubDate>Wed, 15 Jan 2025 00:00:00 +0000</pubDate><guid>https://osmarpetry.dev/blog/react-forwardref/</guid><description>&lt;p&gt;By default, React does not let a parent component access the DOM node of a child component. &lt;code&gt;forwardRef&lt;/code&gt; breaks that boundary explicitly — the child opts in to exposing its DOM node (or instance) to whoever holds the ref.&lt;/p&gt;&#10;&lt;p&gt;The typical need is imperative control from a parent: focusing an input inside a design-system &lt;code&gt;&amp;lt;Input&amp;gt;&lt;/code&gt; wrapper, scrolling a &lt;code&gt;&amp;lt;List&amp;gt;&lt;/code&gt; to a specific row, or triggering animations on a child element. Without &lt;code&gt;forwardRef&lt;/code&gt;, the parent would need to own the DOM directly, which defeats the purpose of encapsulation.&lt;/p&gt;</description></item><item><title>This Object Inside Call, Apply and Bind</title><link>https://osmarpetry.dev/blog/javascript-this-object-inside-call-apply-and-bind/</link><pubDate>Wed, 15 Jan 2025 00:00:00 +0000</pubDate><guid>https://osmarpetry.dev/blog/javascript-this-object-inside-call-apply-and-bind/</guid><description>&lt;p&gt;JavaScript has four rules that determine what &lt;code&gt;this&lt;/code&gt; refers to inside a function: implicit binding, explicit binding (&lt;code&gt;.call&lt;/code&gt;, &lt;code&gt;.apply&lt;/code&gt;, &lt;code&gt;.bind&lt;/code&gt;), &lt;code&gt;new&lt;/code&gt; binding, and window binding. Understanding these rules is essential because &lt;code&gt;this&lt;/code&gt; behaves differently depending on how and where a function is called — not where it is defined.&lt;/p&gt;&#10;&lt;p&gt;At &lt;strong&gt;AutoLot&lt;/strong&gt;, the car dealership platform, the team kept running into bugs where &lt;code&gt;this&lt;/code&gt; was &lt;code&gt;undefined&lt;/code&gt; or pointed to &lt;code&gt;window&lt;/code&gt; instead of the expected object. Once they mapped each scenario to one of the four binding rules, those bugs disappeared.&lt;/p&gt;</description></item><item><title>Testing Concepts Notes</title><link>https://osmarpetry.dev/blog/testing-concepts-notes/</link><pubDate>Fri, 15 Jan 2021 00:00:00 +0000</pubDate><guid>https://osmarpetry.dev/blog/testing-concepts-notes/</guid><description>&lt;h2 id="bdd"&gt;&lt;strong&gt;BDD&lt;/strong&gt;&lt;/h2&gt;&#10;&lt;p&gt;BDD (Behavior Driven Development) is a method of testing where you use &amp;ldquo;Given&amp;rdquo; for the scenario, &amp;ldquo;When&amp;rdquo; for the environment of the action, and &amp;ldquo;Then&amp;rdquo; for the action.&lt;/p&gt;&#10;&lt;p&gt;Imagine &lt;strong&gt;AutoLot&lt;/strong&gt;, a car dealership chain building an online vehicle marketplace. A product manager, a developer, and a QA engineer sit together and write this scenario in plain English:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-fallback" data-lang="fallback"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Given a customer browsing the SUV category&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;When the customer clicks &amp;#34;Schedule Test Drive&amp;#34; on a 2024 Honda CR-V&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Then the test drive request should appear as &amp;#34;pending&amp;#34; in the customer&amp;#39;s dashboard&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This is BDD in practice — the three roles agree on the expected behavior before any code is written. The npm package &lt;code&gt;jest-cucumber&lt;/code&gt; lets you map these Gherkin-style scenarios directly to Jest tests.&lt;/p&gt;</description></item><item><title>Testing Library vs Enzyme</title><link>https://osmarpetry.dev/blog/testing-library-vs-enzyme/</link><pubDate>Fri, 15 Jan 2021 00:00:00 +0000</pubDate><guid>https://osmarpetry.dev/blog/testing-library-vs-enzyme/</guid><description>&lt;h2 id="introduction"&gt;&lt;strong&gt;Introduction&lt;/strong&gt;&lt;/h2&gt;&#10;&lt;p&gt;Testing Library is built around a simple philosophy: the more your tests resemble the way your software is used, the more confidence they can give you. Its three core principles are: tests only break when your app breaks (not when implementation details change), you interact with your app the same way as your users, and built-in selectors find elements the way users do to help you write inclusive code.&lt;/p&gt;</description></item></channel></rss>