<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Victor Toschi on Medium]]></title>
        <description><![CDATA[Stories by Victor Toschi on Medium]]></description>
        <link>https://medium.com/@victortoschi?source=rss-968b8c17855d------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*edjYQZnvlmukcqJP5Zeayg.jpeg</url>
            <title>Stories by Victor Toschi on Medium</title>
            <link>https://medium.com/@victortoschi?source=rss-968b8c17855d------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 11:02:16 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@victortoschi/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Improving the maintainability of your JavaScript code with Functional Programming]]></title>
            <link>https://javascript.plainenglish.io/improving-the-maintainability-of-your-javascript-code-with-functional-programming-3ced76bfae8c?source=rss-968b8c17855d------2</link>
            <guid isPermaLink="false">https://medium.com/p/3ced76bfae8c</guid>
            <category><![CDATA[code-maintainability]]></category>
            <category><![CDATA[functional-programming]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Victor Toschi]]></dc:creator>
            <pubDate>Mon, 28 Apr 2025 02:17:19 GMT</pubDate>
            <atom:updated>2025-04-30T06:48:34.148Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*G4jd4oP1HYjUwGsL" /><figcaption>Photo by <a href="https://unsplash.com/@afgprogrammer?utm_source=medium&amp;utm_medium=referral">Mohammad Rahmani</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Writing code that works is one thing; writing code that stays clean, understandable, and easy to maintain as your application grows is another challenge entirely.<br>Functional Programming offers a powerful set of tools and principles that can help you manage complexity and build JavaScript applications that scale gracefully. In this article, we’ll uncover how Functional Programming can transform not just your code, but your approach to development.</p><h4>What is Functional Programming?</h4><p>Functional Programming is a programming paradigm that emphasises the construction of software using <strong>pure functions</strong>, avoiding <strong>shared state</strong>, <strong>mutable data</strong>, and <strong>side effects</strong>.</p><p>In object-oriented programming, data and behaviour are combined inside objects. In functional programming, computation is the evaluation of pure functions, where state flows through without being changed.</p><p>Functional programming is also <strong>declarative</strong> rather than <strong>imperative</strong>;<strong> </strong>instead of describing <em>how</em> to perform tasks, you describe <em>what</em> you want to achieve.</p><h4>How can it help write maintainable code?</h4><p>Functional code tends to be more <strong>concise</strong>, <strong>predictable</strong>, and <strong>easier to test</strong> than imperative code. Here are some of the benefits:</p><p><strong>Improved Readability: <br></strong>Functional programming makes code more readable and easier to understand. Since functions are modular and reusable, it’s easy to understand how a program works just by reading the code.<br>Example:</p><pre>// Imperative<br>const numbers = [1, 2, 3];<br>let doubled = [];<br>for (let i = 0; i &lt; numbers.length; i++) {<br>  doubled.push(numbers[i] * 2);<br>}<br><br>// Functional (clearer)<br>const numbers = [1, 2, 3];<br>const doubled = numbers.map(n =&gt; n * 2);</pre><p><strong>Increased Modularity: <br></strong>Pure functions and immutability naturally lead to modular design, where components are easier to update, test, and reuse without unintended side effects.</p><p><strong>Fewer Bugs:<br></strong>Functional programming reduces the number of bugs in code since pure functions have no side effects and are easier to reason about.</p><h4>What are the key features of Functional Programming?</h4><p>Here are some core concepts that define the functional approach:</p><p><strong>Pure Functions:</strong><br>A pure function, given the same input, will always produce the same output without modifying any external state.</p><p>Example:</p><pre>// Pure function<br>const add = (a, b) =&gt; a + b;<br><br>// Always same output for same inputs<br>add(2, 3); // 5</pre><p><strong>Higher-Order Functions:</strong><br>Functions that take other functions as arguments or return them as results. Higher-order functions enable powerful patterns like function composition and abstraction.</p><p>Example:</p><pre>const greet = name =&gt; `Hello, ${name}!`;<br><br>const processUser = (user, callback) =&gt; {<br>  return callback(user.name);<br>};<br><br>processUser({ name: &quot;Alice&quot; }, greet); // &quot;Hello, Alice!&quot;</pre><p><strong>Immutability:</strong><br>In functional programming, data is never modified after it’s created. Instead of altering existing structures, new structures are produced with the necessary changes.</p><p>Example:</p><pre>// Mutable<br>let person = { name: &quot;Alice&quot; };<br>person.name = &quot;Bob&quot;;<br><br>// Immutable (functional way)<br>const person = { name: &quot;Alice&quot; };<br>const updatedPerson = { ...person, name: &quot;Bob&quot; };</pre><p><strong>No Side Effects:</strong><br>Functions should avoid interacting with the outside world (like modifying global variables or performing I/O) except at the boundaries of the application.</p><p><strong>Currying:</strong><br>The process of breaking down a function that takes multiple arguments into a series of functions that each take a single argument. This enhances flexibility and reusability.</p><p>Example:</p><pre>const multiply = a =&gt; b =&gt; a * b;<br><br>const double = multiply(2);<br>double(5); // 10</pre><p><strong>Function Composition:</strong><br>Building complex functionality by combining simpler functions. In JavaScript, tools like .map(), .filter(), and .reduce() encourage a compositional style.</p><p>Example:</p><p>Suppose you have an array of numbers, and you want to:</p><ul><li>Double each number.</li><li>Keep only the even results.</li><li>Sum them all together.</li></ul><p>You can <strong>compose</strong> these small operations using .map(), .filter(), and .reduce():</p><pre>const numbers = [1, 2, 3, 4, 5];<br><br>// Compose the steps:<br>const result = numbers<br>  .map(n =&gt; n * 2)          // Double each number<br>  .filter(n =&gt; n % 2 === 0) // Keep only even numbers<br>  .reduce((acc, n) =&gt; acc + n, 0); // Sum them<br><br>console.log(result); // 20</pre><p><strong>Declarative vs. Imperative:</strong><br>Declarative programming focuses on describing <em>what</em> you want to do, rather than <em>how</em> to do it. Functional programming leans heavily toward the declarative style, resulting in more expressive and less error-prone code.</p><h4>Conclusion</h4><p>Adopting functional programming practices in your JavaScript projects can lead to cleaner, more maintainable, and more reliable codebases.</p><p>By emphasising pure functions, immutability, and a declarative style, you create code that’s easier to reason about, extend, and debug.</p><p>If you’re interested in diving even deeper into this subject, a highly recommended resource is Kyle Simpson’s book <strong>“Functional-Light JavaScript”</strong>, which explores practical approaches to functional programming without overcomplicating the concepts.</p><h3>Thank you for being a part of the community</h3><p><em>Before you go:</em></p><ul><li>Be sure to <strong>clap</strong> and <strong>follow</strong> the writer ️👏<strong>️️</strong></li><li>Follow us: <a href="https://x.com/inPlainEngHQ"><strong>X</strong></a> | <a href="https://www.linkedin.com/company/inplainenglish/"><strong>LinkedIn</strong></a> | <a href="https://www.youtube.com/@InPlainEnglish"><strong>YouTube</strong></a> | <a href="https://newsletter.plainenglish.io/"><strong>Newsletter</strong></a> | <a href="https://open.spotify.com/show/7qxylRWKhvZwMz2WuEoua0"><strong>Podcast</strong></a> | <a href="https://differ.blog/inplainenglish"><strong>Differ</strong></a> | <a href="https://twitch.tv/inplainenglish"><strong>Twitch</strong></a></li><li><a href="https://cofeed.app/"><strong>Check out CoFeed, the smart way to stay up-to-date with the latest in tech</strong></a> <strong>🧪</strong></li><li><a href="https://differ.blog/"><strong>Start your own free AI-powered blog on Differ</strong></a> 🚀</li><li><a href="https://discord.gg/in-plain-english-709094664682340443"><strong>Join our content creators community on Discord</strong></a> 🧑🏻‍💻</li><li>For more content, visit <a href="https://plainenglish.io/"><strong>plainenglish.io</strong></a> + <a href="https://stackademic.com/"><strong>stackademic.com</strong></a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3ced76bfae8c" width="1" height="1" alt=""><hr><p><a href="https://javascript.plainenglish.io/improving-the-maintainability-of-your-javascript-code-with-functional-programming-3ced76bfae8c">Improving the maintainability of your JavaScript code with Functional Programming</a> was originally published in <a href="https://javascript.plainenglish.io/">JavaScript in Plain English</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Unit Testing UI Components]]></title>
            <link>https://javascript.plainenglish.io/unit-testing-ui-components-48b335d723eb?source=rss-968b8c17855d------2</link>
            <guid isPermaLink="false">https://medium.com/p/48b335d723eb</guid>
            <category><![CDATA[unit-testing]]></category>
            <category><![CDATA[tdd]]></category>
            <category><![CDATA[testing-library]]></category>
            <category><![CDATA[vuejs]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Victor Toschi]]></dc:creator>
            <pubDate>Wed, 07 Feb 2024 10:44:17 GMT</pubDate>
            <atom:updated>2024-02-08T04:22:24.709Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/894/0*PsszAY7hM__0ifGl.jpeg" /></figure><p>Unit testing is a valuable practice in software development that can significantly <a href="https://www.researchgate.net/publication/3249271_Guest_Editors&#39;_Introduction_TDD--The_Art_of_Fearless_Programming">reduce the number of bugs by 40% — 80%</a>. It also offers other benefits, such as:</p><ul><li>Improvement of your application architecture and maintainability.</li><li>Enhancement of the developer experience by prioritizing better APIs and composability before implementation details.</li><li>Boost confidence when adding new features or refactoring existing ones.</li></ul><p>However, not all aspects of an application are equally easy to unit test. They are particularly effective for testing pure functions, which always return the same output given the same input, and have no side effects.</p><p>In the context of UI components, they often don’t fit into the category of easy to unit test, making it challenging to adopt the test-driven development (TDD) discipline of writing tests first.</p><p>Writing tests first is important to get the benefits mentioned earlier. It takes practice and discipline to adopt TDD, as most developers prefer to code first before writing tests. If we skip the initial test writing, we end up missing some valuable features of unit tests.</p><p>Despite the challenges, practising TDD with unit tests can help simplify UI components, improve maintainability, and enhance their composability and reusability with other components.</p><p>Regardless of the testing framework used, the following tips can help in writing better and more testable UI components:</p><h3>Prefer presentational components</h3><p>These components always render the same output given the same props. If state is required, we can wrap presentational components with container components, which are responsible for managing state and side effects.<br>Example in <a href="https://vuejs.org/">Vue.js</a> and <a href="https://testing-library.com/">Testing Library</a>:</p><pre>&lt;template&gt;<br>  &lt;div class=&quot;greeting&quot;&gt;Hello, {{ userName }}!&lt;/div&gt;<br>&lt;/template&gt;<br><br>&lt;script setup&gt;<br>defineProps([&#39;userName&#39;])<br>&lt;/script&gt;</pre><p>These kinds of components are generally very easy to test. You will need a way to select the component (in this case, we’re using the getByText method from the Testing Library), and you’ll need to know the expected output.</p><pre>import { render, screen } from &#39;@testing-library/vue&#39;;<br>import Hello from &#39;./hello.vue&#39;;<br><br>describe(&#39;Hello component&#39;, () =&gt; {<br>  test(&#39;renders a greeting to the correct username&#39;, () =&gt; {<br>    render(Hello, {<br>      props: {<br>        userName: &#39;Spiderman&#39;<br>      }<br>    });<br>    screen.getByText(&#39;Hello, Spiderman!&#39;);<br>  });<br>});</pre><p>But that’s not very interesting. What if you need to test a stateful component or a component with side effects? That’s where TDD gets interesting for UI components because the answer to that question is the same as the answer to another important question: “How can I make my UI components more maintainable and easy to debug?”</p><p>The answer: Isolate your state and side effects from your presentation components. You can do that by encapsulating your state and side-effect management in a container component and then passing the state into a pure component through props.</p><p>But didn’t the composition API (for Vue.js, and hooks API for React) make it so that we can have flat component hierarchies and forget about all that component nesting stuff? Well, not quite. It’s still a good idea to keep your code in three different buckets, and keep these buckets isolated from each other:</p><ul><li><strong>Display/UI Components</strong></li><li><strong>Program logic/business rules</strong> — the stuff that deals with the problem you’re solving for the user.</li><li><strong>Side effects</strong> (I/O, network, disk, etc.)</li></ul><p>If you keep the display/UI concerns separate from program logic and side effects, it makes your life a lot easier.</p><p>Let’s demonstrate stateful components by creating a click counter. First, we will create the UI component. It should display something like “Times clicked: 2” to tell you how many times a button has been clicked. The button will say “Click”.</p><p>Unit tests for the display component are pretty easy. We only need to test that the button gets rendered (we don’t care about what the label says, it may say different things in different languages, depending on user locale settings). We <em>do </em>want to make sure that the correct number of clicks gets displayed. Let’s write two tests: one for the button display and one for the number of clicks to be rendered correctly.</p><p>We will create a component called &lt;ClickCounter&gt;, and that component will have a prop for the click count, called clicks. To use it, simply render the component and set the clicks prop to the number of clicks you want it to display.</p><p>Let’s look at a pair of unit tests that could ensure we are pulling the click count from props. Let’s create a new file, click-counter/click-counter.test.ts:</p><pre>import { render, fireEvent, screen } from &#39;@testing-library/vue&#39;<br>import ClickCounter from &#39;./click-counter.vue&#39;;<br><br>describe(&#39;ClickCounter component&#39;, async () =&gt; {<br>  render(ClickCounter, { props: count: 2});<br><br>  test(&#39;renders the click button&#39;, async () =&gt; {<br>    screen.getByTestId(&#39;click-button&#39;);<br>  });<br><br>  test(&#39;renders the correct number of clicks&#39;, async () =&gt; {<br>    screen.getByText(&#39;Times clicked: 2&#39;);<br>  });<br>});</pre><p>With the tests written, it’s time to create our ClickCounter component:</p><pre>&lt;template&gt;<br>  &lt;div&gt;<br>    &lt;p&gt;Times clicked: {{ count }}&lt;/p&gt;<br>    &lt;button data-testid=&quot;click-button&quot;&gt;Click&lt;/button&gt;<br>  &lt;/div&gt;<br>&lt;/template&gt;<br><br>&lt;script setup&gt;<br>defineProps&lt;{<br>  count: number;<br>}&gt;();<br>&lt;/script&gt;</pre><p>You will notice the tests are passing. Now we need to implement the state logic and hook up the event handler.</p><h3>Unit Testing Stateful Components</h3><p>The approach I will show you is overkill for a click counter, but most apps are far more complex than a click counter. The state is often shared between components or saved to a database. The popular approach is to start with a local component state and then lift it to a parent component or global app state on an as-needed basis.</p><p>As we are using Vue.js in our example, we will share the state using the Composition API. First, let’s create a new test file for the composable and place it in the same folder. We are calling this one click-counter/composables/use-click-counter.test.ts:</p><pre>import { useClickCounter } from &#39;./click-counter/composables/use-click-counter&#39;;<br><br>describe(&#39;useClickCounter composable&#39;, async () =&gt; {<br>  test(&#39;increases the counter on call&#39;, () =&gt; {<br>    const { counter, increase } = useClickCounter();<br>  <br>    expect(counter.value).toBe(0);<br>  <br>    increase();<br>  <br>    expect(counter.value).toBe(1);<br>  });<br>});</pre><p>We start with an assertion to ensure that the counter variable will have a valid initial state. Then, we call the increment() method and assert the counter variable again to check the new value.</p><p>Now let’s make the test pass:</p><pre>export function useClickCounter() {<br>  const counter = ref(0);<br><br>  function increase() {<br>    counter.value += 1;<br>  }<br><br>  return { counter, increase }<br>}</pre><p>Just one more step: connecting our behaviour to our component. We can do that with a container component. We will call that click-counter-container.vue and place it with the other files. It should look like this:</p><pre>&lt;template&gt;<br>  &lt;ClickCounter :count=&quot;counter&quot; @onClick=&quot;increase&quot; /&gt;<br>&lt;/template&gt;<br><br>&lt;script setup&gt;<br>import ClickCounter from &#39;./click-counter/click-counter.vue&#39;;<br>import { useClickCounter } from &#39;./click-counter/composables/use-click-counter&#39;;<br><br>const { counter, increase } = useClickCounter();<br>&lt;/script&gt;</pre><p>That’s it. This component’s only job is to connect our state and pass it in as props to our unit-tested pure component. To test it, load the app in your browser and click the ‘click’ button.</p><p>We will now hook up the increase function in the ClickCounter component by emitting a click event to the parent component:</p><pre>&lt;template&gt;<br>  &lt;div&gt;<br>    &lt;p&gt;Times clicked: {{ count }}&lt;/p&gt;<br>    &lt;button data-testid=&quot;click-button&quot; @click=&quot;onClick&quot;&gt;Click&lt;/button&gt;<br>  &lt;/div&gt;<br>&lt;/template&gt;<br><br>&lt;script setup&gt;<br>defineProps&lt;{<br>  count: number;<br>}&gt;();<br>defineEmits([&#39;onClick&#39;]);<br>&lt;/script&gt;</pre><p>And all the unit tests still pass.</p><p>What about tests for the container component? We don’t have to unit test container components. Instead, we can use end-to-end tests which simulate user interactions with the actual UI.</p><p>We need both kinds of tests (unit and end-to-end) in our applications, and writing unit tests for container components (which are mostly connective components like the one that wires up our composable function above) would be too redundant with end-to-end tests and not particularly easy to unit test properly. Often, you would have to mock various container component dependencies to get them to work.</p><p>Meanwhile, we have unit-tested all the important units that don’t depend on side effects: we tested that the expected data gets rendered and that the state is managed correctly. We should also load the component in the browser and see that the button works and the UI responds.</p><p>Developers can effectively test their UI components by adopting these guidelines, leading to more robust and maintainable applications.</p><h3>In Plain English 🚀</h3><p><em>Thank you for being a part of the </em><a href="https://plainenglish.io/"><strong><em>In Plain English</em></strong></a><em> community! Before you go:</em></p><ul><li>Be sure to <strong>clap</strong> and <strong>follow</strong> the writer ️👏<strong>️️</strong></li><li>Follow us: <a href="https://twitter.com/inPlainEngHQ"><strong>X</strong></a><strong> | </strong><a href="https://www.linkedin.com/company/inplainenglish/"><strong>LinkedIn</strong></a><strong> | </strong><a href="https://www.youtube.com/channel/UCtipWUghju290NWcn8jhyAw"><strong>YouTube</strong></a><strong> | </strong><a href="https://discord.gg/in-plain-english-709094664682340443"><strong>Discord</strong></a><strong> | </strong><a href="https://newsletter.plainenglish.io/"><strong>Newsletter</strong></a></li><li>Visit our other platforms: <a href="https://stackademic.com/"><strong>Stackademic</strong></a><strong> | </strong><a href="https://cofeed.app/"><strong>CoFeed</strong></a><strong> | </strong><a href="https://venturemagazine.net/"><strong>Venture</strong></a></li><li>More content at <a href="https://plainenglish.io/"><strong>PlainEnglish.io</strong></a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=48b335d723eb" width="1" height="1" alt=""><hr><p><a href="https://javascript.plainenglish.io/unit-testing-ui-components-48b335d723eb">Unit Testing UI Components</a> was originally published in <a href="https://javascript.plainenglish.io/">JavaScript in Plain English</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Rest Parameter Syntax & Spread Operator in JavaScript]]></title>
            <link>https://javascript.plainenglish.io/rest-parameter-syntax-spread-operator-in-javascript-be8e83e9f02c?source=rss-968b8c17855d------2</link>
            <guid isPermaLink="false">https://medium.com/p/be8e83e9f02c</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[javascript-tips]]></category>
            <dc:creator><![CDATA[Victor Toschi]]></dc:creator>
            <pubDate>Sun, 13 Nov 2022 18:10:54 GMT</pubDate>
            <atom:updated>2022-11-17T01:16:55.166Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LEd1UG-Q1awNWeLLbaF1oQ.jpeg" /></figure><p>In this article, we are going to explore the triple dots (...) syntax that JavaScript provides. Its mechanisms, and why they are not operators.</p><p>Essentially, the triple dot syntax is used for a couple of mechanisms:</p><ul><li>Rest syntax for receiving data.</li><li>Spreading for sending data.</li></ul><h3>Receiving data: rest parameter syntax</h3><p>A rest parameter is a peculiar kind of parameter that receives all remaining arguments of a function call via an Array:</p><pre>function foo(first, ...rest) { <br>  return { first, rest } <br>}</pre><pre>foo(&#39;a&#39;, &#39;b&#39;, &#39;c&#39;);</pre><pre>// { first: &#39;a&#39;, rest: [ &#39;b&#39;, &#39;c&#39; ] }</pre><p>We can also use the rest syntax in Array-destructuring:</p><pre>const [head, ...tail] = [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;];</pre><pre>head<br>//&#39;a&#39;</pre><pre>tail<br>// [ &#39;b&#39;, &#39;c&#39; ]</pre><p>And we can use it in object-destructuring:</p><pre>const { first: f, ...rest} = {<br>  first: &#39;Petter&#39;, <br>  last: &#39;Parker&#39;, <br>  age: 19<br>};</pre><pre>f<br>// &#39;Petter&#39;</pre><pre>rest<br>// { last: &#39;Parker&#39;, age: 19 }</pre><h3>Sending data: spread operator</h3><p>Using the spread operator in a function call turns Array elements into function call arguments.</p><pre>function returnArgArray(...args) { <br>  return args;<br>}</pre><pre>returnArgArray(...[&#39;x&#39;, &#39;y&#39;, &#39;z&#39;]);<br>//[ &#39;x&#39;, &#39;y&#39;, &#39;z&#39; ]</pre><p>We can also spread Arrays into Array literals:</p><pre>[...[&#39;a&#39;, &#39;b&#39;], &#39;c&#39;]<br>// [ &#39;a&#39;, &#39;b&#39;, &#39;c&#39; ]</pre><p>And we can spread objects into object literals:</p><pre>{...{ a:1, b:2 }, c:3}<br>// { a: 1, b: 2, c: 3 }</pre><h3>Rest and spread are not operators</h3><p>Operators such as + or await are used to write independent expressions that evaluate values. We can use those expressions in many contexts.</p><p>In contrast, rest and spread are part of their surroundings. That’s why they shouldn’t be called operators:</p><ul><li>Rest syntax: rest parameters (function definitions), rest elements (Array-destructuring), rest properties (object-destructuring)</li><li>With spreading, I usually say: “spreading into …” (function calls, Array literals, object literals).</li></ul><h3>Further reading (use cases, etc.)</h3><p>Rest syntax:</p><ul><li><a href="https://exploringjs.com/impatient-js/ch_callables.html#rest-parameters">Rest parameters</a></li><li><a href="https://exploringjs.com/impatient-js/ch_destructuring.html#rest-elements">Destructuring via rest elements</a></li><li><a href="https://exploringjs.com/impatient-js/ch_destructuring.html#rest-properties">Destructuring via rest properties</a></li></ul><p>Spreading:</p><ul><li><a href="https://exploringjs.com/impatient-js/ch_callables.html#spread-arguments">Spreading into function calls</a></li><li><a href="https://exploringjs.com/impatient-js/ch_arrays.html#spreading-into-array-literals">Spreading into Array literals</a></li><li><a href="https://exploringjs.com/impatient-js/ch_objects.html#spreading-into-object-literals">Spreading into object literals</a></li></ul><p><em>More content at </em><a href="https://plainenglish.io/"><strong><em>PlainEnglish.io</em></strong></a><em>. Sign up for our </em><a href="http://newsletter.plainenglish.io/"><strong><em>free weekly newsletter</em></strong></a><em>. Follow us on </em><a href="https://twitter.com/inPlainEngHQ"><strong><em>Twitter</em></strong></a>, <a href="https://www.linkedin.com/company/inplainenglish/"><strong><em>LinkedIn</em></strong></a><em>, </em><a href="https://www.youtube.com/channel/UCtipWUghju290NWcn8jhyAw"><strong><em>YouTube</em></strong></a><em>, and </em><a href="https://discord.gg/GtDtUAvyhW"><strong><em>Discord</em></strong></a><em>. Interested in Growth Hacking? Check out </em><a href="https://circuit.ooo/"><strong><em>Circuit</em></strong></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=be8e83e9f02c" width="1" height="1" alt=""><hr><p><a href="https://javascript.plainenglish.io/rest-parameter-syntax-spread-operator-in-javascript-be8e83e9f02c">Rest Parameter Syntax &amp; Spread Operator in JavaScript</a> was originally published in <a href="https://javascript.plainenglish.io/">JavaScript in Plain English</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to Use Decorators in JavaScript]]></title>
            <link>https://medium.com/@victortoschi/using-decorators-in-javascript-e80674e4c6fa?source=rss-968b8c17855d------2</link>
            <guid isPermaLink="false">https://medium.com/p/e80674e4c6fa</guid>
            <category><![CDATA[decorators]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[web-development]]></category>
            <dc:creator><![CDATA[Victor Toschi]]></dc:creator>
            <pubDate>Tue, 10 May 2022 00:02:03 GMT</pubDate>
            <atom:updated>2022-11-13T19:32:02.667Z</atom:updated>
            <content:encoded><![CDATA[<h3>How to use Decorators in JavaScript</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nIO9zHIGk94uWLQ-XwRd9g.png" /></figure><h3>What are decorators?</h3><p>Essentially,<strong> </strong>the<strong> decorator</strong> is a design pattern that allows behaviour to be added to an individual object, either statically or dynamically, without modifying the underlying piece of code.</p><p>You may also have heard of this concept as functional composition or higher-order functions. Therefore, we can achieve it in plain JavaScript for numerous use-cases by calling on one function to wrap another:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*F9Vs-19wk6AwTu8cvUFidA.png" /></figure><p>The code above assigns a new function to the variable wrapped that can be called exactly the same way as the printName function, and will do exactly the same thing. The difference is that it will do some logging before and after the wrapped function is called:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*O9iCcRWikQ8JlpGDwMgkhA.png" /></figure><h3>Decorators in JavaScript and how to use them</h3><p>Decorators are not a standard JavaScript feature. They are in stage 3 of the ECMA TC39 <a href="https://github.com/tc39/proposal-decorators">specification phase</a>. To use JavaScript Decorators we need to set up a transpiler tool in our development environment, like <a href="https://babeljs.io/">Babel</a>.</p><p>In JavaScript, we can apply decorators to <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/Public_class_fields">class fields</a>, methods, and the entire class. Therefore, we can’t use decorators on plain objects. They only work with classes.</p><p>Decorators use a special syntax where they are prefixed with a @ symbol and placed before the code being decorated.</p><p>Let’s look at how we can decorate class properties, class methods and entire classes.</p><h4>Decorating class fields</h4><p>In the example below, we can see how to use the decorator syntax on a class field:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oKG31olPCaUAq3PT7NXNZg.png" /></figure><p>In the code above, we have a decorator function called locked that we apply to the password class field. It takes in three parameters: target, key, and <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty">descriptor</a>. The target is the object or function that is being decorated and the key refers to the property name on that target. The descriptor contains all of the properties of the target, including those that are being decorated.</p><p>Class field decorators work by returning a new descriptor. So we return a new descriptor object with all of the properties and values from the original except the writable flag set to false. This prevents changing the password field after it&#39;s been instantiated.</p><p>Now, if we try changing our password field:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ljXiziy_gc1mn3ENWSSwbg.png" /></figure><p>As we can see, attempting to change the password field causes an error because now it&#39;s a read-only property.</p><h4>Decorating class methods</h4><p>Let’s take a look at an example of using a decorator on a method:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6mlV7OkElo0Rm1CJqxhBaw.png" /></figure><p>The decorated method takes a descriptor and returns an object that contains the original method plus any additional logic.</p><p>In this example, we are adding some error handling to our getData method. If anything goes wrong with our request, it will be caught and logged to the console.</p><h4>Decorating a class</h4><p>Below we can see how to apply a decorator to the entire class:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*v52ez4XoslMckKyeVSEhJA.png" /></figure><p>The @storeInCache decorator is applied to our class, and it will automatically store any new instances of User in a cache map.</p><h3>Conclusion</h3><p>JavaScript <strong>decorators</strong> are a powerful feature that provides a good way of writing simple helper code that can be applied to a lot of places in a very clean and easy-to-read manner, which saves you time and reduces redundancy in your code. Take advantage of it if you are working with classes, and remember it is in the stage 3 proposal of TC39.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e80674e4c6fa" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to create a Slot Machine animation with CSS and JavaScript]]></title>
            <link>https://medium.com/@victortoschi/how-to-create-a-slot-machine-animation-with-css-and-javascript-9073ab9db9ea?source=rss-968b8c17855d------2</link>
            <guid isPermaLink="false">https://medium.com/p/9073ab9db9ea</guid>
            <category><![CDATA[html]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[css-animation]]></category>
            <category><![CDATA[animation]]></category>
            <category><![CDATA[tutorial]]></category>
            <dc:creator><![CDATA[Victor Toschi]]></dc:creator>
            <pubDate>Sat, 20 Nov 2021 17:17:31 GMT</pubDate>
            <atom:updated>2021-11-20T17:17:31.945Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*tUBECs-CoIw4oI4f" /><figcaption>Photo by <a href="https://unsplash.com/@aysha_be?utm_source=medium&amp;utm_medium=referral">Ays Be</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>I have been playing with CSS Animations, and I came up with a simple <a href="https://codepen.io/toschivictor/pen/JjNZjEj">Slot Machine</a> animation.</p><p>Below I will share a few details about the implementation.</p><h4>DOM structure</h4><p>Firstly, we need to create the HTML elements for our Slot Machine. We will display the results in three different boxes:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d3cee5e6c615b09bdfe195db92c20b55/href">https://medium.com/media/d3cee5e6c615b09bdfe195db92c20b55/href</a></iframe><h4>Styling the document</h4><p>Next step, we will create the base CSS:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8c0610e5d73f3e62ff944ad0842eca10/href">https://medium.com/media/8c0610e5d73f3e62ff944ad0842eca10/href</a></iframe><h4>Handling the animation with JavaScript</h4><p>In our JS file we will create three functions to handle the animation:</p><ul><li>init(): the main method that initialises the animation for each box element and handles the transition events.</li><li>spin(): responsible for setting the spin animation.</li><li>shufle(): returns a shuffled array.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d6405f566d520518a5c442f82cdc4d01/href">https://medium.com/media/d6405f566d520518a5c442f82cdc4d01/href</a></iframe><p>Final result:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodepen.io%2Ftoschivictor%2Fembed%2Fpreview%2FJjNZjEj%3Fdefault-tabs%3Djs%252Cresult%26height%3D600%26host%3Dhttps%253A%252F%252Fcodepen.io%26slug-hash%3DJjNZjEj&amp;display_name=CodePen&amp;url=https%3A%2F%2Fcodepen.io%2Ftoschivictor%2Fpen%2FJjNZjEj&amp;image=https%3A%2F%2Fshots.codepen.io%2Ftoschivictor%2Fpen%2FJjNZjEj-800.jpg%3Fversion%3D1637427&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codepen" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/447eab9cdde2c62d16888a1082bc3fb7/href">https://medium.com/media/447eab9cdde2c62d16888a1082bc3fb7/href</a></iframe><p>The base implementation is fairly simple and we can easily and in new functionalities.</p><p>Thanks for reading!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9073ab9db9ea" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>