<?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 Eamon O&#39;Callaghan on Medium]]></title>
        <description><![CDATA[Stories by Eamon O&#39;Callaghan on Medium]]></description>
        <link>https://medium.com/@eamonocallaghan?source=rss-43293ac2726a------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*vZuThBAMWmORHRpJ7PJswA.jpeg</url>
            <title>Stories by Eamon O&amp;#39;Callaghan on Medium</title>
            <link>https://medium.com/@eamonocallaghan?source=rss-43293ac2726a------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 16 Nov 2026 03:26:50 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@eamonocallaghan/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[Running NestJS Queues in a Separate Process]]></title>
            <link>https://medium.com/s1seven/running-nestjs-queues-in-a-separate-process-948f414c4b41?source=rss-43293ac2726a------2</link>
            <guid isPermaLink="false">https://medium.com/p/948f414c4b41</guid>
            <category><![CDATA[nestjs]]></category>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[queue]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[bull-queues]]></category>
            <dc:creator><![CDATA[Eamon O'Callaghan]]></dc:creator>
            <pubDate>Thu, 26 Sep 2024 13:43:42 GMT</pubDate>
            <atom:updated>2024-09-27T09:45:57.863Z</atom:updated>
            <content:encoded><![CDATA[<h4>Maintaining Dependency Injection and Scalability</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cvpjXj48ikWcMVlQuWKO3A.jpeg" /><figcaption>Multiple workers working alongside a main process.</figcaption></figure><p>Queues are crucial for handling long-running tasks like certificate notarization and email sending in distributed systems. At <a href="https://s1seven.com/">S1Seven</a>, we rely heavily on queues. However, as the load on our systems grew, we encountered performance bottlenecks — our queue-heavy process began overloading and stopped accepting new requests.</p><p>By default, NestJS queues run in the same process as the application, which can cause performance issues if tasks become CPU-intensive. In this article, we’ll explore how to split queues into separate processes in NestJS while maintaining dependency injection, allowing for greater scalability and performance so your application can handle increased load smoothly.</p><h3>The default NestJS approach</h3><p>According to the current documentation at <a href="https://docs.nestjs.com/techniques/queues">https://docs.nestjs.com/techniques/queues</a>, when creating a NestJS app with a queue, the queue shares the same process as the application by default. That means that if you have CPU-intensive code in the queue, it could block the event loop, preventing your application from handling new requests.</p><p>Ideally, queues should be run on a separate process, allowing the main process to handle any incoming requests.</p><h3>Splitting queues out to run on a separate process</h3><p>The <a href="https://docs.nestjs.com/techniques/queues#separate-processes-1">NestJS documentation</a> touches on this point but only provides a few details. If you look at their example of a queue that runs on a separate process (reproduced below), you will notice that it is just a function rather than a class.</p><p>Here is what the NestJS documentation currently recommends :</p><pre>// app.module.ts<br><br>@Module({<br>  imports: [<br>    BullModule.forRootAsync({<br>      useFactory: () =&gt; ({<br>        connection: {<br>          url: process.env.REDIS_URL || &#39;redis://localhost:6379&#39;,<br>        },<br>      }),<br>    }),<br>    BullModule.registerQueue({<br>      name: &#39;audio&#39;,<br>      processors: [join(__dirname, &#39;processor.js&#39;)],<br>    }),<br>  ],<br>})<br>export class AppModule {}</pre><pre><br>// processor.ts<br><br>// Here we use bullmq instead of bull as bull is now in maintenance mode<br>import { Job } from &#39;bullmq&#39;;<br><br>export default async function (job: Job): Promise&lt;string&gt; {<br>  // here you have access to the full job object<br>}</pre><p>The file path to this processor function is specified in the app.module.ts file. The advantage of this way of running the queue on a forked process is that NestJS takes care of running this file in another process for you. However, as this function is run in a forked process, dependency injection is not available.</p><blockquote>Since the function runs in a separate process, it doesn’t share the memory space of the main app, including the DI container, which is why you lose access to NestJS-specific features like ConfigService and other providers.</blockquote><p>That means that instead of interfacing with @nestjs/bullmq , you have to interact directly with Bullmq. You also lose access to ConfigService and any other dependencies you may need in your application.</p><h3>The recommended approach</h3><p>A workaround is to create a new app with a new entry point, which can be run manually in parallel. This method allows you to use dependency injection and the @Processor decorator from NestJS, but it requires you to start the process manually.</p><blockquote>By running the worker in a separate process, you’re adopting a microservice pattern. This allows you to scale worker processes independently from your main application and isolate failures.</blockquote><p>Platforms like Heroku and Docker make running separate processes for workers and web servers easy when deploying your application by allowing you to specify different entry points or commands. For instance, Heroku’s Procfile can specify a worker process, and Docker Compose can define multiple services.</p><pre>// worker.ts<br><br>import { NestFactory } from &#39;@nestjs/core&#39;;<br>import { WorkerModule } from &#39;./worker/worker.module&#39;;<br><br>async function bootstrap() {<br>  const app = await NestFactory.create(WorkerModule);<br>  await app.listen(process.env.PORT);<br>}<br>bootstrap();</pre><pre>// worker.module.ts<br><br>import { Module } from &#39;@nestjs/common&#39;;<br>import { AppService } from &#39;../app/app.service&#39;;<br><br>import { AudioConsumer } from &#39;./worker.processor&#39;;<br>import { BullModule } from &#39;@nestjs/bullmq&#39;;<br><br>@Module({<br>  imports: [<br>    BullModule.registerQueue({<br>      name: &#39;audio&#39;,<br>      connection: {<br>        url: process.env.REDIS_URL || &#39;redis://localhost:6379&#39;,<br>      },<br>    }),<br>  ],<br>  providers: [AppService, AudioConsumer],<br>})<br>export class WorkerModule {}</pre><pre>// worker.processor.ts<br><br>import { OnWorkerEvent, Processor, WorkerHost } from &#39;@nestjs/bullmq&#39;;<br>import { Job } from &#39;bullmq&#39;;<br><br>@Processor(&#39;audio&#39;)<br>export class AudioConsumer extends WorkerHost {<br><br>  async process(job: Job&lt;any, any, string&gt;): Promise&lt;any&gt; {<br>    // eslint-disable-next-line no-console<br>    console.log(&#39;worker&#39;, process.pid);<br><br>    // This is an example job that just slowly reports on progress<br>    // while doing no work. Replace this with your own job logic.<br>    let progress = 0;<br><br>    while (progress &lt; 100) {<br>      await sleep(50);<br>      progress += 1;<br>      job.updateProgress(progress);<br>    }<br><br>    // A job can return values that will be stored in Redis as JSON<br>    // This return value is unused in this demo application.<br>    return { value: &#39;This will be stored&#39; };<br>  }<br>}</pre><p>You should also update the app.module.ts BullModule.registerQueue section, removing the processors property.</p><pre>BullModule.registerQueue({<br>  name: &#39;audio&#39;,<br>}),</pre><p>Using the above example, you can compile the TypeScript to JavaScript, and run node main.js to start the main process and node worker.js to start the worker.</p><p>Because the queue audio is registered in the main application and in the worker application, the queue will be shared, and any jobs created in the main app will be processed in the worker app.</p><h3>Conclusion</h3><p>Running NestJS queues in a separate process resolves performance bottlenecks and provides flexibility in scaling worker processes independently. By isolating long-running tasks, your application can remain responsive to incoming requests while still managing queues efficiently.</p><p>At <a href="https://s1seven.com/">S1Seven</a>, adopting this approach allowed us to solve our queue overloading issues and scale quickly. If you’re facing similar challenges, consider implementing this in your project.</p><p>See <a href="https://github.com/eamon0989/nestjs-worker-queue">https://github.com/eamon0989/nestjs-worker-queue</a> for a working example. It showcases how to quickly set up and run worker processes alongside a main NestJS application.</p><p>I hope you’ve found this article helpful. Check out some of my other articles on development <a href="https://medium.com/@eamonocallaghan"><strong>here</strong></a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=948f414c4b41" width="1" height="1" alt=""><hr><p><a href="https://medium.com/s1seven/running-nestjs-queues-in-a-separate-process-948f414c4b41">Running NestJS Queues in a Separate Process</a> was originally published in <a href="https://medium.com/s1seven">S1SEVEN</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[What is Node-Red and how can I install it?]]></title>
            <link>https://medium.com/s1seven/what-is-node-red-and-how-can-i-install-it-e59eb0518dc4?source=rss-43293ac2726a------2</link>
            <guid isPermaLink="false">https://medium.com/p/e59eb0518dc4</guid>
            <category><![CDATA[node-red]]></category>
            <category><![CDATA[s1seven]]></category>
            <dc:creator><![CDATA[Eamon O'Callaghan]]></dc:creator>
            <pubDate>Wed, 16 Nov 2022 09:01:48 GMT</pubDate>
            <atom:updated>2022-11-21T14:30:10.469Z</atom:updated>
            <content:encoded><![CDATA[<h4>A simple introduction to Node-Red</h4><p>Node-Red is a “low-code” programming solution. It allows you to create simple programs or “flows” that can be used to accomplish many simple programming tasks with little or no programming knowledge. It provides a browser-based UI where wires can be used to connect nodes.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hDbd45eKnegIpJbsyZSnkw.png" /><figcaption>Sample Node-Red flow</figcaption></figure><p>The image above shows a simple flow, illustrating how Node-Red works. Notice the wires connecting nodes. A flow generally starts with a blue input node. In this flow, it’s called Verify Certificate. The input node is used to start a flow (similar to a start button). In this case, it is used to inject a JSON certificate into the flow.</p><p>The purpose of this flow is to send a JSON certificate to the S1Seven servers and verify whether or not that certificate has been notarized on the blockchain.</p><p>The input data then flows along the wires between nodes from one node to the next. In this case, the second node sends an HTTP request (similar to what happens when you open a webpage) to the S1Seven servers. This particular node has been configured to send a JSON certificate to the S1Seven servers, and then pass the data that is sent back by S1Seven to the json node.</p><p>The json node takes JSON data as input and parses it as an object (a common machine-readable data structure used in programming). Then that parsed data is finally passed to the green node, which simply prints the data that has been passed into it.</p><p>To use Node-Red, it is not necessary to know all the details of how each node works, you simply need to select the nodes that do what you need and connect them to your flow.</p><p>The best way to learn to use Node-Red is to dive right in and play with it.</p><h3>Installing Node-Red</h3><p>There are many ways to install Node-Red, including in Azure or AWS: <a href="https://nodered.org/docs/getting-started/">https://nodered.org/docs/getting-started/</a>. This guide will show you how to install Node-Red locally on your machine.</p><h3>Mac:</h3><p>Node-Red depends on a program called Node. Node is an environment that allows you to run JavaScript code on your machine. First, you need to download and install Node from the following link — be sure to download the current LTS version:</p><p><a href="https://nodejs.org/en/#home-downloadhead">Node.js</a></p><p>Once you’ve downloaded the file, open it and follow the installation instructions to install Node. Once Node is installed, open the Terminal app and type in node --version to check that node is correctly installed. It should output something similar to v16.14.2 if it’s successfully installed.</p><p>Now we are able to continue installing Node-Red. Still in the Terminal app, input this text: sudo npm install -g --unsafe-perm node-red.</p><p>Now hit enter to start the installation. You will know that it is installed successfully when you see something like the following text:</p><pre>+ node-red@1.1.0<br>added 332 packages from 341 contributors in 18.494s<br>found 0 vulnerabilities</pre><p>Once it is installed, type node-red into the terminal to run Node-Red. Then open your browser of choice and navigate to localhost:1880 which is the default access point for Node-Red.</p><h3>Windows:</h3><p>Node-Red depends on a program called Node. Node is an environment that allows you to run JavaScript code on your machine. First, you need to download and install Node from the following link — be sure to download the current LTS version:</p><p><a href="https://nodejs.org/en/download/">Node.js - Download Node.js®</a></p><p>Once you’ve downloaded the file, open it and follow the installation instructions to install Node. Once you get to the following step, tick the box and continue with the installation. This will allow you to later install node packages that are compiled from C/C++, which may be needed to install some custom nodes for Node-Red.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yTZ12yPteW3F8Co5Fs1urA.png" /><figcaption>Tools for Native Modules option</figcaption></figure><p>Once Node has finished installing, if you have checked the box as directed above, the following window should open automatically:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1qgkW_m64ThG_2ajFeq52w.png" /><figcaption>Tools for Native Modules installation</figcaption></figure><p>Press any key to continue, and follow the instructions until the software is installed.</p><p>To verify that Node has been correctly installed, press the Windows key + R and type cmd to open the command window.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2V2YhX3V2v561HuaAxMWlw.png" /><figcaption>The Windows run box</figcaption></figure><p>Once the cmd window is open, type node --version &amp;&amp; npm --version and check that the output looks something like v16.15.0 as you can see in the screenshot below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FBGLHaHwDskUjnOzXklOog.png" /><figcaption>Verify that Node and npm have been installed</figcaption></figure><h3>Installing Node-Red</h3><p>While still in the cmd window, type npm install -g --unsafe-perm node-red which will install Node-Red. Once it is installed, type node-red into the console to run Node-Red.</p><p>The first time that you run it, you may be prompted to allow Node access by Windows Defender on your local network, you should grant access by clicking the Allow access button.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DWJRFteIE-qqhHKybPEK5Q.png" /><figcaption>Allow Node access on Private networks</figcaption></figure><p>Now Node-Red should be up and running! Open your browser of choice and navigate to localhost:1880 which is the default access point for Node-Red. You should see something similar to the image below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_xqGo0IRtgIz6TK38HXY7A.png" /><figcaption>Node-Red initial screen</figcaption></figure><h3>Getting started</h3><p>The editor window contains three main sections:</p><ul><li>The <a href="https://nodered.org/docs/user-guide/editor/palette">palette</a> on the left contains the nodes available to use.</li><li>The main <a href="https://nodered.org/docs/user-guide/editor/workspace">workspace</a> is in the middle, where flows are created.</li><li>The <a href="https://nodered.org/docs/user-guide/editor/sidebar">sidebar</a> on the right shows information about flows, nodes, or the debug window.</li></ul><p>To get started, drag a blue Inject node from the palette on the left to the workspace in the middle. Also, drag a green Debug node to the workspace.</p><p>Click on the little grey dot on the right-hand side of the Inject node (its output port) to the little grey dot on the left-hand side of the Debug node (its input port). This will connect the two nodes with a wire, allowing data to flow between them.</p><p>Then click the red Deploy button on the top right of the screen. Now your changes are active and the flow can be used. In the sidebar to the right, click on the Debug tab (the third icon, the one that looks like a bug). Then click the inject button on the blue Inject node to start your flow. It should look similar to the screenshot below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qmJi1m4Io3yR9f38ztYt1A.png" /><figcaption>First Node-Red flow</figcaption></figure><p>The numbers to the right indicate the number of milliseconds since January 1st, 1970. You can click on the numbers to change the format to a more readable date. And that is the basics of how Node-Red works!</p><p>Of course, you want to be able to do more than just inject and print timestamps. The next section will show how you can interact with objects and data in Node-Red!</p><h3>Interacting with objects</h3><p>Node-Red works by sending msg (message) objects from one node to the next. If you are not familiar with the concept of objects from programming, you can think of them as being a bit like an email. We generally think of an email as one entity, but an email is made up of several fields: to, from, cc, body, etc.</p><p>A msg object can be likened to that email in that it is one entity, but it can have multiple properties (fields). To illustrate, double-click on the blue Inject node. Here you can see the properties of the msgobject. By default, msg.payload will be set to a timestamp.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mUxLA6zjufBLbbaArU96vw.png" /><figcaption>msg.payload</figcaption></figure><p>As you can see in the screenshot, there is a little + add button underneath the properties. Click on that button to add a new property. You can rename the property however you like. As an example, let’s simulate an email and set 3 properties, to, from, and body.</p><p>Click on the grey drop-down menu to the right of the = sign to choose what data type you want the property to be. There are several options including string (which represents text), number, and JSON. Select string, and type in email@example.com. Set the properties to the values seen in the screenshot below and click Done when finished.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pCT21Xa_caVVNwijyRAdHw.png" /><figcaption>Setting properties on the msg object</figcaption></figure><p>Before we do anything with that data, you need to understand how to access the data. Look at the green debug node. Double-click on it, and notice that the Output is set to msg.payload. That notation means that you want to access the payload property on the msg object.</p><p>If, for example, you wanted to access the to property that you have set on the msg object, you could change payload to to. However, we want to see all the properties on the msg object, not just one. So beside Output, click the grey msg dropdown and choose complete msg object as you can see in the following screenshot:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*h-hgqsg0tnKp9yuQvF2IJQ.png" /><figcaption>Select complete msg object</figcaption></figure><p>Now click the red Done button and click the Deploy on the top right to save your changes. Now when you click the inject button on the blue Inject node, instead of injecting a timestamp, the msg object will be sent to the debug node and printed in the debug section on the right side of your screen.</p><p>To improve the formatting, click once on the printed object. It should look similar to the screenshot below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sZEsLjid3vaYzrUCntQVbA.png" /><figcaption>Our printed msg object to the right</figcaption></figure><p>As you can see, the msg object has four properties:</p><ul><li>_msgid — An internal id generated by Node-Red, you can ignore this.</li><li>to — The email that you added</li><li>from — The email that you added</li><li>body — The text that you added.</li></ul><p>Now that you know the basics of how flows and objects work in Node-Red, you can start playing around with some of our pre-made flows that interact with the S1Seven API or show use-cases for JSON certificates.</p><h3>Links to some of our pre-made flows</h3><p>To see flows related to interacting with the S1Seven API, click <a href="https://medium.com/s1seven/interacting-with-the-s1seven-api-via-our-custom-node-red-module-13cc7b1a8539">here</a>.</p><p>To see flows showing how incoming certificates can be saved to Azure, click <a href="https://medium.com/s1seven/saving-certificates-to-azure-using-node-red-4f8028bd511c">here</a>.</p><p>To see flows that show how to extract properties from JSON certificates and plot them to a graph, click <a href="https://medium.com/s1seven/creating-a-scatter-chart-using-a-folder-of-json-certificates-d677e91491f4">here</a>.</p><p>To see a flow that shows you how to use S1Seven’s certificate tools to convert a JSON certificate to a PDF, click <a href="https://medium.com/s1seven/use-s1sevens-certificate-tools-to-render-a-json-certificate-as-a-pdf-d60c44073b4d">here</a>.</p><h3>Conclusion</h3><p>Thank you for reading this article. For more information, please contact us at <a href="https://s1seven.com/">https://s1seven.com</a> or support@s1seven.com.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e59eb0518dc4" width="1" height="1" alt=""><hr><p><a href="https://medium.com/s1seven/what-is-node-red-and-how-can-i-install-it-e59eb0518dc4">What is Node-Red and how can I install it?</a> was originally published in <a href="https://medium.com/s1seven">S1SEVEN</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Why you should be using Git Branches in LS 101]]></title>
            <link>https://medium.com/launch-school/why-you-should-be-using-git-branches-in-ls-101-ee2864009f07?source=rss-43293ac2726a------2</link>
            <guid isPermaLink="false">https://medium.com/p/ee2864009f07</guid>
            <category><![CDATA[git-branch]]></category>
            <category><![CDATA[learning-to-code]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[git]]></category>
            <category><![CDATA[launch-school]]></category>
            <dc:creator><![CDATA[Eamon O'Callaghan]]></dc:creator>
            <pubDate>Sat, 14 Aug 2021 13:01:11 GMT</pubDate>
            <atom:updated>2021-08-14T13:23:59.917Z</atom:updated>
            <content:encoded><![CDATA[<h4>A step by step guide to the magical world of Git Branching</h4><figure><img alt="A picture of code with git branches" src="https://cdn-images-1.medium.com/max/1024/1*RpdCB8ssHVk6dpHL31rKbA.jpeg" /></figure><blockquote>“It is easy to shoot your foot off with git, but also easy to revert to a previous foot and merge it with your current leg.”</blockquote><blockquote><em>— Jack William Bell</em></blockquote><h3>Why you should be using git branching</h3><p>While working through Launch School, you will end up working on games such as Rock Paper Scissors, Tic-Tac-Toe, and other similar projects. That wonderful moment finally arrives when your code is working as it should and you can happily move on to the next lesson. Then you see that you have to add some bonus features to the game. OK, that sounds like fun.</p><p>Inevitably, you start to add new features and end up breaking your code. This is normal, we’ve all been there. What most beginners will do is Ctrl + Z (undo) until the code starts working again, but now you’ve lost all your progress and it’s a very messy way to undo changes.</p><h3>The magic of <strong>git branching</strong></h3><p>What if I told you there was an almost magical way to try out new things, add new features, and not have to worry about breaking your code in the process? Well, today is your lucky day. Thanks to the magic of <strong>git branching</strong>, you can try out new features and undo them at any time. You can restore your code to a working state with just a few short commands. So how does it work?</p><blockquote>I’m going to assume you have gone through the <a href="https://launchschool.com/books/git/read/introduction">Introduction to Git</a> book that is required reading in the Launch School prep phase, and that you are committing your code regularly and pushing it to GitHub. If not, now is the best time to start!</blockquote><p>Every time you make a git commit, you are saving a <strong>snapshot</strong> of your code. This allows you to <strong>branch</strong> your code, which essentially means you can work on a copy of your code without affecting your main branch. It’s not actually making a copy but that’s the easiest way to think about it.</p><h3>How to use git branches</h3><blockquote>If you use VS Code, the easiest way to follow these instructions is to open your project folder, click on the ‘Terminal’ tab at the top of the screen and select ‘New Terminal’.</blockquote><p>I’m going to use my project as an example. I’m currently working on my project Launch School Time Estimator, and I want to add a new feature. I type the command git status into the terminal and I get this output:</p><pre>[eamon@fedora lstimeestimate]$ git status<br>On branch main<br>Your branch is up to date with &#39;origin/main&#39;.</pre><pre>nothing to commit, working tree clean</pre><p>This message means that all my changes have been committed. If you get a different message, that means you have to add and commit your code before continuing. You can do that by typing git add . and git commit -m &#39;insert your message here&#39;.</p><blockquote>Note that I will put a reference list of all the necessary commands at the bottom of this article.</blockquote><h3>Creating a new branch</h3><p>To create a new branch, I type git checkout -b development into the console, and in response git prints the message: Switched to a new branch ‘development’. You can name the branch whatever you like, for the purposes of this article I’m using development.</p><blockquote>git checkout -b development is shorthand for git branch development and git checkout development, it creates a new branch and switches to it in one command instead of having to write both commands out separately.</blockquote><p>Using a new branch means that any changes I make to this branch will not affect the main branch. So I’m free to experiment without having to worry about messing up my code. But happens if I mess up and want to restore my original code? All I have to do is switch back to the main branch and delete the development branch.</p><p>I can do that by typing git checkout main into the console. Now my changes have disappeared and my code is restored to the working snapshot we saved earlier. And I can delete the development branch by typing git branch -d development.</p><h3>Merging changes back to main</h3><p>OK, that’s simple enough, but what if I add a new feature in the development branch and everything works as expected? In that case, I can <strong>merge</strong> the development branch into the main branch, which incorporates any changes I’ve made back into the main branch.</p><p>You first have to commit your changes to the development branch if you haven’t already done so, so run git add . to stage all your files for the commit, then type git commit -m &#39;insert your commit message here&#39;. Your new feature has now been saved to your development branch and all we need to do is merge it back into the main branch.</p><p>To do so, first switch back to the main branch by typing git checkout main. Now your newly added code has disappeared, but that’s OK as this is what allows you to undo any mistakes if necessary. Now to merge branches and incorporate your new features from the development branch to the main branch type git merge development.</p><p>You should see output something like this:</p><pre>[eamon@fedora lstimeestimate]$ git merge development<br>Auto-merging app.js<br>Merge made by the &#39;recursive&#39; strategy.<br> app.js | 4 +++-<br> 1 file changed, 3 insertions(+), 1 deletion(-)</pre><p>Now your main branch contains the same code as your development branch, and you can feel free to push it to GitHub or to wherever you store your code.</p><blockquote>Note that if you delete a line of code in your development branch and then merge that branch, it will also delete that line from your main branch. So branches can be used both to add and remove code.</blockquote><p>Now that your main branch and development branch both contain the updated code with your new features, you can delete the development branch by running git branch -d development.</p><h3>Conclusion</h3><p>You can open as many branches as you like and you can even open different branches for different features, but that is beyond the scope of this article. For now, practice using git branches every time you add a new feature to your project, soon it will become second nature.</p><p>Now is the best time to get comfortable with using git branches as they are an essential part of a developer&#39;s workflow. Feel free to message me on Slack if you have any questions.</p><h3>List of all the git commands contained in this article:</h3><ul><li>git add . — Stage your files, preparing them for the next commit.</li><li>git commit -m &#39;your message here&#39; — Commit your changes, creating a snapshot of your code.</li><li>git checkout -b development — Creates a new development branch and switches to it. This is shorthand for the following two commands.</li><li>git branch development — Creates a new branch called development.</li><li>git checkout development — Switches you over to the development branch.</li><li>git checkout main — Switch back to the main branch.</li><li>git merge development — Merge the changes from development to the current branch.</li><li>git branch -d development — Delete the development branch.</li><li>git branch -D development — Force delete the development branch.</li><li>git status — Shows you the status of your current branch.</li></ul><p>If you want to learn about some of the more technical details of how branching works, take a look at this article: <a href="https://git-scm.com/book/en/v2/Git-Branching-Branches-in-a-Nutshell">https://git-scm.com/book/en/v2/Git-Branching-Branches-in-a-Nutshell</a></p><p>Check out some of my other articles on development, Launch School, and JavaScript <a href="https://medium.com/@eamonocallaghan"><strong>here</strong></a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ee2864009f07" width="1" height="1" alt=""><hr><p><a href="https://medium.com/launch-school/why-you-should-be-using-git-branches-in-ls-101-ee2864009f07">Why you should be using Git Branches in LS 101</a> was originally published in <a href="https://medium.com/launch-school">Launch School</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The this keyword in JavaScript made simple.]]></title>
            <link>https://medium.com/launch-school/the-this-keyword-in-javascript-made-simple-320eaddecc22?source=rss-43293ac2726a------2</link>
            <guid isPermaLink="false">https://medium.com/p/320eaddecc22</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[execution-context]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[launch-school]]></category>
            <category><![CDATA[this-keyword]]></category>
            <dc:creator><![CDATA[Eamon O'Callaghan]]></dc:creator>
            <pubDate>Mon, 09 Aug 2021 12:49:00 GMT</pubDate>
            <atom:updated>2021-08-10T20:38:21.218Z</atom:updated>
            <content:encoded><![CDATA[<h4>Execution context and how the value of this is determined.</h4><figure><img alt="You got this" src="https://cdn-images-1.medium.com/max/1024/1*BU36_SYn4QiWxy4JD_Cu_g.jpeg" /></figure><p>The idea behind the this keyword is that when you invoke a method that contains the this keyword in its body, this points to an object and tells the method where to look to find the properties needed in the method. The value of this can also be referred to as the <strong>execution context </strong>of a function or method.</p><p><strong>What you need to know</strong>:</p><ol><li>Outside of a function body, this points to <strong>the global object</strong>.</li><li>Regular function calls use <strong>the global object</strong> as their execution context.</li><li>Method calls use <strong>the calling object</strong> as their execution context.</li><li>How this is bound depends on <strong>how</strong> a function is invoked rather than on where it’s defined.</li></ol><p>Let’s examine each of those in more detail:</p><h3>Outside of a function</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/908/1*5W2TUYL4vAlrvAKnc2fgjQ.png" /></figure><p>The most important thing to know when determining the value of this in JavaScript is that if this appears <strong>outside</strong> of the body of a function or method, it points to the global object.</p><p>The <a href="https://developer.mozilla.org/en-US/docs/Glossary/Global_object">global object</a> is an object that always exists on the global scope or the top level of your code. Object, Array, Functionand other built-in constructors are all properties of the global object.</p><h3>Inside of a function</h3><p>When you invoke a regular function that contains the this keyword in its body, this points to the global object.</p><blockquote><em>this</em> points to the global object inside a function, but in strict mode it points to <em>undefined</em>.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nynTeKxKjSVdIkKHasSppA.png" /></figure><h3>Example</h3><p>In the following code block the function printString will look for the properties subject and adjective. So based on what we have just learned, what will happen when we invoke the function printString below?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5UoY2Z4of_MfqWS35gauow.png" /></figure><p>Why did this.subject and this.adjective evaluate to undefined? As printString was invoked as a standalone function, its execution context or this binding was implicitly set to the global object.</p><p>So printString looked at the global object, searched for a property named subject, and as there is no property named subject on the global object it evaluated to undefined. Then the same thing happened with this.adjective.</p><p>To prove that is what’s happening, we can add those two properties to the global object and see what happens.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*P2qYNXbuxhdGcahdD--ANQ.png" /></figure><p>Notice that on lines 5 and 6 we add the properties subject and adjective to the global object. Now when we invoke printString on line 8, it finds those properties and appends their values to the string.</p><blockquote>Note: adding properties to the global object is <strong>not</strong> recommended. I’ve done it here to illustrate a point. Don’t do this in your code.</blockquote><p>Also, note that the above code will <strong>not</strong> work in strict mode. Remember that in strict mode, this will point to undefined in a regular function call.</p><p><strong>Summary</strong>:</p><ul><li>If this appears outside of the body of a function, it points to the global object.</li><li>Regular function calls use the global object as their execution context.</li></ul><h3>Using this in methods</h3><p>So far we have seen how this is bound in a standalone function but that’s not very useful. When this really comes into play is when we use it in object methods. Let’s add our printString function to an object and call it as a method.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BvSaV7t-qpRPKzcewUFm-A.png" /></figure><p>In this example, the method printString looks for two properties, subject and adjective. How does it know where to look for them?</p><p>You may think that it just looks for those properties in the same object that the method appears in. That is not quite accurate though. Instead, it looks for the properties <strong>in the object that calls the method</strong>.</p><p>That is a very important distinction. What would happen if we stored object.printString in a variable and then called it?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gZAYQa-DRv5hTo8cziETIg.png" /></figure><p>Notice that we have stored object.printString in a variable called func and then invoked func on line 10. Now suddenly this is no longer pointing to object. Can you guess where it is pointing?</p><p>If you said the global object, you were correct. func is called as a standalone function on line 10 and not as a method, and as we learned earlier the implicit execution context of a function is the global object.</p><p>This teaches us something very important about the binding of this. The value of this is determined, not by where it appears in your code, but by <strong>how</strong> a function or method is invoked. Let’s see how we can get func working correctly again.</p><p><strong>Summary</strong>:</p><ul><li>Method calls use the calling object as their execution context.</li><li>You can’t tell the binding of this just by looking at the body of a function or method, you must look at <strong>how</strong> it is invoked.</li></ul><h3>Explicitly setting the context using call</h3><p>So far we have mentioned several times how this is <strong>implicitly</strong> set when you invoke a function or method. That implies that we can also <strong>explicitly</strong> set the execution context.</p><p>All functions have access to a property method named call that they inherit from Function.prototype. We can invoke the call method on a function and explicitly set the execution context to whatever object we pass in as the first argument.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EeV0GN5hSDgOMfg2HZ7i_Q.png" /></figure><p>Notice that we invoke the func function on line 15 with the call method, and we pass in anotherObject as an argument. Now the execution context of func on line 15 is bound to the object anotherObject, and so it looks there for the properties subject and adjective.</p><p>Note that using call does not mutate func, so we can still call func as a standalone function and its execution context will still be implicitly bound to the global object. But what if we wanted to permanently bind anotherObject as the execution context of func?</p><h3>Permanently binding an object</h3><p>All functions also have access to a property method named bind that they inherit from Function.prototype. bind works differently from call in that it doesn’t invoke the function. Instead, it returns a <strong>new</strong> function with its execution context permanently bound to the passed in argument.</p><p>Notice how on line 15 we store the new function returned by bind to the variable newFunc.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XfHoZjLPE-LLuePRB9Ni1Q.png" /></figure><p>When we invoke newFunc on line 16, we see that its execution context has been bound to anotherObject. It’s no longer possible to change the execution context of newFunc as it has been permanently bound to anotherObject. Note that func has <strong>not</strong> been mutated.</p><p><strong>Summary:</strong></p><ul><li>We can explicitly set the execution context using call or bind.</li><li>call invokes the function with its execution context set to the passed in argument.</li><li>bind returns a <strong>new</strong> function with the execution context permanently bound to the passed in argument.</li></ul><h3>Why arrow functions are special</h3><p>Now that you’ve taken all this time to learn how this is bound inside in functions, so you may be surprised to learn to arrow functions do <strong>not </strong>have their own this binding.</p><p>Notice that in the following code block, we’ve replaced our printString method with an arrow function.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DIFD2Ul463gDDwWqY1cWjg.png" /></figure><p>When invoked, this.subject and this.adjective evaluate to undefined. This is because arrow functions do<strong> not</strong> have their own this binding, instead, they establish the binding of this from the surrounding code.</p><p>We know that when this is not in a function body, it points to the global object (or undefined in strict mode). Therefore, in this case, this points to the global object.</p><p>Once an arrow function is defined in your code, its execution context cannot be changed. If the arrow funtion is defined within the body of another function, it will always have the same context as the outer function.</p><blockquote>Unlike with non-arrow functions, the execution context for arrow functions is determined lexically, which means that we determine the context from <strong>where</strong> the arrow function is defined rather than from how it is invoked.</blockquote><p>As arrow functions do not get their own this binding, the methods call and bind will <strong>not</strong> work.</p><p>This may sound like a negative, but it is actually a very useful design feature as we will now see.</p><h3>Dealing with context loss using Arrow functions</h3><p>Let’s say we want to use an array method in our object within the printProps method. Notice on line 5 that we call forEach on anArray and we try to print out the elements of anotherArray.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WGdmL4PazQnee1yOnZ1Lqg.png" /></figure><p>Why did we get a TypeError? It turns out that when we pass a function as an argument to another function, we lose access to this in the inner function. This is called ‘<strong>context loss</strong>’. So when we pass an anonymous function as an argument to forEach on line 5, this on line 6 points to the global object rather than to object.</p><p>There are a few ways to fix this including using bind, but this is where arrow functions really shine! In the following example, we change our anonymous function to an arrow function on line 5:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mOZwoCnAVxbBBkHYPbEzZw.png" /></figure><p>Now it works! Why? Remember, arrow functions do not get their own this binding, instead they use this from the surrounding code. As the arrow function is in the body of printProps, it uses the this binding of printProps which we know to be object.</p><p>So while arrow functions are not suitable for use as methods, they can be very useful when we need a function within a function or when we need to pass a function as an argument to another function.</p><h3>Summary</h3><ul><li>Execution context refers to the value of the this keyword.</li><li>Outside of a function body, this points to the global object.</li><li>Regular function calls use the global object as their execution context.</li><li>Method calls use the calling object as their execution context.</li><li>In strict mode, this is implicitly set to undefined rather than the global object within regular function calls.</li><li>Arrow functions do not have their own execution context, they use the context of their surrounding scope.</li><li>You can’t tell the binding of this just by looking at the body of a function or method, you must look at <strong>how</strong> it is invoked.</li></ul><p>Thanks to <a href="https://launchschool.com/course_catalog">Launch School</a> where I studied all of this in far more detail. Feel free to check out some of my other articles on JavaScript and my journey through Launch School <a href="https://medium.com/@eamonocallaghan"><strong>here</strong></a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=320eaddecc22" width="1" height="1" alt=""><hr><p><a href="https://medium.com/launch-school/the-this-keyword-in-javascript-made-simple-320eaddecc22">The this keyword in JavaScript made simple.</a> was originally published in <a href="https://medium.com/launch-school">Launch School</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[What is duck typing in JavaScript?]]></title>
            <link>https://medium.com/launch-school/what-is-duck-typing-in-javascript-f3eb10853361?source=rss-43293ac2726a------2</link>
            <guid isPermaLink="false">https://medium.com/p/f3eb10853361</guid>
            <category><![CDATA[launch-school]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[duck-typing]]></category>
            <category><![CDATA[polymorphism]]></category>
            <dc:creator><![CDATA[Eamon O'Callaghan]]></dc:creator>
            <pubDate>Fri, 30 Jul 2021 13:43:38 GMT</pubDate>
            <atom:updated>2021-08-09T04:04:24.735Z</atom:updated>
            <content:encoded><![CDATA[<h4>Why does JavaScript think that this picture shows a duck typing and why should you care?</h4><figure><img alt="Cat typing on a keyboard." src="https://cdn-images-1.medium.com/max/1024/1*N02fJ5AaQbyzQT9LcI1zLw.jpeg" /></figure><p>JavaScript thinks that this is a picture of a duck typing. No seriously, it does. And as far as we are concerned if JavaScript thinks that this is a picture of duck typing, we can also assume it to be a picture of a duck typing.</p><p>What do I mean by this? Well, <strong>duck typing</strong> means that if two or more unrelated objects respond to the same <strong>method</strong> <strong>name</strong>, they are all ducks. Even if they aren’t. But as far as JavaScript is concerned they are, and if JavaScript thinks they are all ducks and we get the desired output, they might as well all be ducks.</p><p>So with regards to the picture, as far as JavaScript is concerned if an object has and responds to a type method, it’s a duck.</p><h3>Proof that a cat is really a duck</h3><p>Let’s test that theory. In the following code block we have two classes, Duck and Cat. They are completely unrelated in any way. We then create two new objects on lines 11 and 12, duck which is an instance of Duck and cat which is an instance of Cat.</p><figure><img alt="Proof that a cat is really a duck" src="https://cdn-images-1.medium.com/max/1024/1*Ck1vh1eogcM7bqBkSZWlQg.png" /></figure><p>Notice that in our code block when we invoke the makeDucksType function on line 9 with possibleDuckImposter referencing the object duck,we get the intended result. Therefore duck is a duck. When we invoke the makeDucksType function with possibleDuckImposter referencing the object cat, we also get the intended result. Therefore both duck and cat are ducks.</p><blockquote>Duck typing is an informal way of classifying objects that are not related to each other that respond to the same method name. Instead of saying ‘all these objects respond to method x, they must be of type y’, we say ‘all these objects respond to method x, they must be ducks.</blockquote><p>If thinking of them both as ducks doesn’t make sense to you, you can also think of it in this way:</p><p>duck and cat are both objects. Both of them respond to the method type . Therefore as far as JavaScript is concerned both objects are of the same type. You can call this informal grouping of unrelated objects x, group, or even duck.</p><h3>Making a dog quack</h3><p>In this example for some reason, our programmer wants to make a dog quack. Maybe he’s making a game or just likes weird animals. Who am I to judge?</p><p>Look at the output on line 14 of our code block:</p><figure><img alt="Making dogs quack" src="https://cdn-images-1.medium.com/max/1024/1*filypeXGuB_40OWh-hmD4w.png" /></figure><p>Javascript checks:</p><ul><li>Does the object referenced by possibleDuckImposter (duck) have a quack method? Yes. Must be a duck.</li></ul><p>So it quacks.</p><p>Then JavaScript checks:</p><ul><li>Does the object referenced by possibleDuckImposter (dog) have a quack method? Yes. Must be a duck.</li></ul><p>And so it quacks. Notice that even though Dog has a method called quack, it didn’t print ducks but instead printed dog emojis. This is still an example of duck typing as the method has the same name and it returns the desired result.</p><blockquote>“If it walks like a duck and it quacks like a duck, then it must be a duck”. Or in this case, <strong>if it has a </strong>quack<strong> method it must be a duck</strong>. Obviously not literally, but JavaScript doesn’t know that.</blockquote><p>So how can we actually use this concept in our code, assuming of course that we don’t just want to make dogs quack?</p><h3>An actual useful example of duck typing</h3><p>Take a look at the following code block. This code is adapted from code found in the Launch School forums, thanks, Pete! I’ll link the source at the end of the article.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hjL70rVDNg8wpYX6rC8qxw.png" /></figure><p>We are calling forEach on an array that contains elements of two different types, a string and an array. Both elements get passed into the callback in turn and are in turn stored in or referenced by the parameter obj. Then the method indexOf checks if &#39;e&#39; is found and returns the index. JavaScript doesn’t care that our elements have different types. It just cares about finding the index of &#39;e&#39;.</p><blockquote>Duck typing means we don’t have to explicitly declare types and we assume that the method will work the way we want it to.</blockquote><p>What actually happens behind the scenes here is a little more complicated. When the value of obj is &#39;aeiou&#39;, JavaScript invokes String.prototype.indexOf, and when obj references the array [&#39;a&#39;, &#39;e&#39;] it invokes Array.prototype.indexOf.</p><p>So JavaScript checks:</p><ul><li>Does String.prototype have an indexOf method? Yes. Must be a duck.</li><li>Does Array.prototype have an indexOf method? Yes. Must be a duck.</li></ul><p>In both instances, we receive the desired outcome.</p><blockquote>If you try to do this in a language that doesn’t support duck typing like C, it will throw a type error. In a language like C you can’t call methods for 2 or more objects within the same line of code unless the objects have a common superclass. — Pete</blockquote><p>Let’s try and insert a third element to our array, this time a number.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mMMZp0DowGJ6vVET4rzv4Q.png" /></figure><p>This time we get a TypeError . Why? Javascript checks:</p><ul><li>Does Number.prototype have an indexOf method? No. Not a duck.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hzliZ3QUXGvXgtiyF94-RQ.jpeg" /></figure><h3>Summary</h3><ul><li>The idea behind <strong>duck typing </strong>is that objects of unrelated types can respond to the same method name and therefore we can treat all the objects that respond to that method name as ducks, or more specifically, as belonging to a specific category of objects.</li><li>We assume that each object’s method will respond correctly just as we assumed that indexOf would return the correct index when we called it on both a string and an array.</li><li>It doesn’t matter to us that the implementation of the method is different in each case. It only matters that it gives us the outcome we expect.</li></ul><p>Here is the <a href="https://launchschool.com/posts/c6a86a52#comment-89887">promised link</a> to the Launch School forum where I found the example, unfortunately, it will only work if you are a student at Launch School and have passed this point in the JavaScript track.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f3eb10853361" width="1" height="1" alt=""><hr><p><a href="https://medium.com/launch-school/what-is-duck-typing-in-javascript-f3eb10853361">What is duck typing in JavaScript?</a> was originally published in <a href="https://medium.com/launch-school">Launch School</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[[[Prototype]] vs __proto__ vs .prototype in Javascript]]></title>
            <link>https://medium.com/@eamonocallaghan/prototype-vs-proto-vs-prototype-in-javascript-6758cadcbae8?source=rss-43293ac2726a------2</link>
            <guid isPermaLink="false">https://medium.com/p/6758cadcbae8</guid>
            <category><![CDATA[object-oriented]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[prototype]]></category>
            <dc:creator><![CDATA[Eamon O'Callaghan]]></dc:creator>
            <pubDate>Fri, 30 Jul 2021 05:38:53 GMT</pubDate>
            <atom:updated>2021-07-30T07:25:46.138Z</atom:updated>
            <content:encoded><![CDATA[<h3>[[Prototype]] vs __proto__ vs .prototype in Javascript</h3><h4>Why are there so many types of prototypes in JavaScript and what do they all do?</h4><figure><img alt="A confused man tries to work out what prototype means." src="https://cdn-images-1.medium.com/max/1024/1*NdxIFtI2AeW3WkTaFePRjA.jpeg" /></figure><p>It can seem very daunting when you start learning about prototypes in JavaScript. A lot of the confusion stems from the fact that there are two different <strong>prototypes</strong> in JavaScript that refer to different concepts. Let me explain.</p><h3>[[Prototype]]</h3><p>[[Prototype]] is a hidden private property that all objects have in Javascript, it holds a reference to the object’s <strong>prototype</strong>.</p><p>An object’s prototype is the object that an object inherits or descends from. In the following diagram the object son descends from father , so father is the prototype of son. That means the hidden [[Prototype]] property of son points to father.</p><figure><img alt="Inheritance diagram" src="https://cdn-images-1.medium.com/max/432/1*hm_7ugR7TLq8RWaV2FuUTA.png" /></figure><p>To summarize:</p><ul><li>[[Prototype]] is a hidden property that references an object’s prototype.</li><li>An object&#39;s prototype is the object that an object descends or inherits from.</li></ul><h3><strong>__proto__</strong></h3><p>__proto__ (also called the Dunder Proto or <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/proto">Double Underscore Prototype</a>) is a property of Object.prototype (more on that in a minute) that exposes the hidden [[Prototype]] property of an object and allows you to access or modify it. You should not use it as it is <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/proto"><strong>deprecated</strong></a>, although you may come across it in older code.</p><p>The modern way of accessing an object’s prototype is by using Object.getPrototypeOf(obj). You can also modify an object’s prototype using Object.setPrototypeOf(obj, prototype) as you can see in the following example:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/982/1*-eS6joous1TTZiuM7XeiyQ.png" /></figure><p>To summarize:</p><ul><li>__proto__ is a method that exposes the hidden [[Prototype]] property and allows you to modify it.</li><li>Object.getPrototypeOf() and Object.setPrototypeOf() are the modern ways of getting access to and setting an object’s prototype.</li></ul><h3>.prototype</h3><p>.prototype is a special property that almost all functions have that is only used when a function is invoked as a constructor function. I say almost all because arrow functions and methods defined using the concise syntax do not have .prototype properties and cannot be used as constructors.</p><p>The .prototype property contains a reference to an object and when a constructor is used to instantiate or create a new object, .prototype is set as the prototype of the new object.</p><blockquote>A constructor function is like an object factory that creates new objects which are instances of itself. “<a href="https://medium.com/@eamonocallaghan/a-simplified-overview-of-javascript-prototypes-and-prototypical-inheritance-f00c7af8a93c#4026"><em>What is a constructor function?</em></a><em>”</em></blockquote><p>For example, when the constructor ObjectFactory is used to instantiate a new object referenced by the variable obj on line 5 of the following code block, obj&#39;s hidden internal [[Prototype]] property now holds a reference to the same object that is referenced by ObjectFactory.prototype. This means that any properties or methods defined in ObjectFactory.prototype are accessible by obj.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*E_lwhJ3_f6uFfdbgpuXvDQ.png" /></figure><p>Here on line 7, we prove that ObjectFactory.prototype references an object. On line 8 we prove that ObjectFactory.prototype is the prototype of obj.</p><h3>Accessing a prototype’s properties</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YV46rudM4p-KMgPJUHa22g.png" /></figure><p>Notice that on line 10 we add a property called prop to the object referenced by ObjectFactory.prototype . Remember that obj’s [[Prototype]] property references the same object as ObjectFactory.prototype.</p><p>We can see on line 12 that obj does not have a property called prop, it only has one property called property. However, obj can access prop on line 13 because it inherits the properties of its prototype which we know is the object referenced by ObjectFactory.prototype.</p><p>To summarize:</p><ul><li>.prototype is a special property that all functions have that contains a reference to an object.</li><li>When a constructor is used to instantiate a new object, ConstructorName.prototype is set as the prototype of the new object.</li><li>All instances of that constructor (the objects it creates) can access the properties of ConstructorName.prototype.</li></ul><p>I briefly mentioned Object.prototype earlier in the article, Object is the built-in constructor function that almost all objects in JavaScript are instances of, and therefore almost all objects inherit from the object referenced by its .prototype property, Object.prototype.</p><p>I hope that this article has helped you to see the difference between these concepts! You can learn more about prototypical inheritance in JavaScript in my other article <a href="https://medium.com/launch-school/a-simplified-overview-of-javascript-prototypes-and-prototypical-inheritance-f00c7af8a93c">here</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6758cadcbae8" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[A simplified overview of JavaScript prototypes and prototypical inheritance.]]></title>
            <link>https://medium.com/launch-school/a-simplified-overview-of-javascript-prototypes-and-prototypical-inheritance-f00c7af8a93c?source=rss-43293ac2726a------2</link>
            <guid isPermaLink="false">https://medium.com/p/f00c7af8a93c</guid>
            <category><![CDATA[launch-school]]></category>
            <category><![CDATA[prototype-chain]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[prototypical-inheritance]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Eamon O'Callaghan]]></dc:creator>
            <pubDate>Mon, 26 Jul 2021 14:41:26 GMT</pubDate>
            <atom:updated>2021-07-30T07:24:09.447Z</atom:updated>
            <content:encoded><![CDATA[<h4>What are the two types of prototypes in JavaScript and what are they used for?</h4><figure><img alt="Macbook showing a code snippet on the screen." src="https://cdn-images-1.medium.com/max/1024/1*WhR0GNM_wrfEawQ4HfU2ZQ.jpeg" /></figure><p>To understand prototypes and inheritance in JavaScript, you will need to understand these five points:</p><ol><li>Objects can have properties.</li><li>Objects inherit properties from their prototype.</li><li>Functions are objects which can also have properties.</li><li>Functions have a prototype property.</li><li>A function’s prototype is NOT the same thing as an object’s prototype.</li></ol><p>Let’s examine each of these points in more detail.</p><p>Disclaimer: The information in this article is information that I have learned from <a href="https://launchschool.com/course_catalog">Launch School</a>, MDN, and <a href="https://youtu.be/-N9tBvlO9Bo">this excellent video</a> by a Launch School student called Ryan Schaul. I am a student at Launch School and I wanted to write an article that presents the basic concepts of prototypes in an easy-to-understand manner. Code snippets are linked at the bottom of the article.</p><h3>Objects properties</h3><p>The first thing you need to know is that objects in JavaScript can have properties. For example, the object foo here has a property called bar:</p><figure><img alt="A code snippet showing an object foo with a property called bar." src="https://cdn-images-1.medium.com/max/1024/1*-t-x7RFVvdPnBDXOR0235w.png" /></figure><p>Its property can be accessed using dot notation as we see on line 5. On line 6 we verify that foo has a property called bar by calling the hasOwnProperty method.</p><h3>Object prototypes and inheritance</h3><p>Objects usually have many properties that you can’t see by simply logging the value of the object. This is because objects can inherit properties from their prototype. The object that another object inherits properties from is called its <strong>prototype</strong>.</p><p>If you need a mental model, you can think of how a child inherits characteristics from its parent. In this example, the parent would be considered the ‘prototype’ and the child the ‘object’.</p><p>An object’s prototype is a private property that each object has that references or points to its prototype (the object that it inherits from). According to the JavaScript documentation at <a href="https://262.ecma-international.org/11.0/#sec-ordinary-object-internal-methods-and-internal-slots">ECMAScript</a>, this internal property is called [[Prototype]].</p><figure><img alt="A code snippet showing that Object.prototype has the property has own property." src="https://cdn-images-1.medium.com/max/1024/1*Y_n8cxcmSQBcJ0Hdrcn6pw.png" /></figure><p>A good example of this is the method hasOwnProperty on line 6. Notice that we call hasOwnProperty using dot notation, which means that hasOwnProperty is itself a property of an object. We prove that on line 7 by checking if Object.prototype has a property called hasOwnProperty which returns true.</p><p>In this case, the object foo inherits the property method hasOwnProperty from Object.prototype and can thus access the method. Object.prototype can be thought of as being like the grandfather of all objects as almost all objects are descendants of Object.prototype and therefore have access to its properties and methods. We will examine Object.prototype in more detail later in this article.</p><p>To summarise:</p><ul><li>Objects have an internal property called [[Prototype]] that points to their prototypes.</li><li>Objects inherit properties and methods from their prototypes.</li><li>Almost all objects are descendants of Object.prototype and have access to its properties and methods.</li></ul><h3>Object prototypes vs Function prototypes</h3><p>There are two types of prototypes in JavaScript which are easily confused. As we have already seen, one is a private property that each object has called [[Prototype]] that references that object’s prototype.</p><p>The other is a .prototype property that almost all functions have that is only used when that function is invoked as a constructor. I say almost all because arrow functions and methods defined using the concise syntax do not have .prototype properties and cannot be used as constructors. But what is a constructor function?</p><p>A constructor function is like an object factory that creates new objects which are instances of itself. You can see an example on line 6 of the following code block, where an object is implicitly returned by Factory and a reference to that object is stored in the descendant variable.</p><figure><img alt="An image showing an object created using the constructor function factory." src="https://cdn-images-1.medium.com/max/1024/1*cYEH3fBPlwLazzX5mwmgzw.png" /></figure><p>We can access Factory&#39;s prototype property using dot notation, Factory.prototype . Upon logging the value of Factory.prototype on line 8 we see that it references an object. What is this object&#39;s purpose?</p><p>Each object instance that is created by Factory inherits from the object referenced by Factory.prototype. We can easily test this.</p><figure><img alt="An image showing that descendants of a constructor function inherit properties of that functions prototype property." src="https://cdn-images-1.medium.com/max/1024/1*5CQd-PmbyKllvyKTBtStMQ.png" /></figure><p>Notice that on line 6 we add a new property called example to the object referenced by Factory.prototype. We use Factory to create a new object instance on line 9 and we store a reference to that object in the variabledescendant.</p><p>When we then log its value on line 10, we see that the properties foo and bar have been assigned to it. Notice that we can also access the property example through descendant on line 11 as it has inherited it from Factory.prototype .</p><p>So what do we learn from this?</p><ul><li>Functions have a property called prototype which holds a reference to an object.</li><li>Any object instances created by a constructor function will inherit the properties of the object at FunctionName.prototype.</li></ul><h3>Function prototypes and Object.prototype</h3><p>So where does the object referenced by Factory.prototype come from? Interestingly it is an object that is a descendant of Object.prototype.</p><p>On line 9 of the following code block we log the value of Factory.prototype&#39;s prototype, which is the object referenced by Object.prototype.</p><figure><img alt="An image showing that a function constructor inherits from Object.prototype" src="https://cdn-images-1.medium.com/max/1024/1*1WFn4s14cdEHhbqYrnzQ5g.png" /></figure><p>We confirm that on line 11 by showing that Object.prototype and Factory.prototype&#39;s prototype both reference the same object and on line 15 where we show that Object.prototype is the prototype of Factory.prototype.</p><figure><img alt="An image showing that descendant objects inherit from Object.prototype" src="https://cdn-images-1.medium.com/max/1024/1*IuSoKtBJuOEWCUwgJqa45g.png" /></figure><p>We test this further by adding the property test to Object.prototype on line 16 which is the default object that almost all other objects inherit from. (This is not something you should do in your code, this is just an example that illustrates a point. I say almost all because it is possible to create an object that doesn’t inherit properties, but that is beyond the scope of this article).</p><p>Then on line 21 we can access that property through Factory.prototypeas Factory.prototype is a descendant of Object.prototype . We can also access the property test from descendant on line 23 as it inherits from Factory.prototype which in turn inherits from Object.prototype.</p><p>To summarize:</p><ul><li>A constructor function’sprototype property is a descendant of the object referenced by Object.prototype.</li></ul><h3>Creating new objects</h3><p>So what happens when you create a new object? When you create an object using the object literal syntax or using a constructor function, you are creating an object that inherits either directly or indirectly from Object.prototype.</p><p>As the Object constructor is itself a function, it has a property called prototype which can be accessed at Object.prototype . Object.prototype references an object, and almost all objects inherit properties from Object.prototype. You can think of inheritance as a chain, each object’s prototype references the next link in the chain until it reaches the default object at the top of the chain, Object.prototype.</p><p>Notice in the following code block we initialize an empty object using the object literal syntax and store a reference to that object in the variable obj.</p><figure><img alt="An image showing that an object created using object syntax literals is a descendant of Object.prototype" src="https://cdn-images-1.medium.com/max/1024/1*D6Pln5poIxoecnESxBVnhA.png" /></figure><p>On line 2 we log the value of Object.prototype which references an object. On line 3 we log the value of obj&#39;s prototype (or parent), which also references an empty object.</p><p>On line 5 we prove that the object referenced by Object.prototype and the prototype of obj are the same. And on line 6 we further prove that Object.prototype is the prototype of obj using the isPrototypeOf method.</p><p>That means that if we mutate the object at Object.prototype by giving it new properties, obj will also be able to access those properties (Again this is not recommended, it’s just being used to illustrate a point here). On line 8 we initialize the property foo of Object.prototype to the string &#39;Hey there!&#39;.</p><p>We can test if the object has been mutated by logging the value of Object.prototype, where we see that the property foo has been successfully added. Now all descendants of Object.prototype have access to foo as we can see on line 11 when we access it from obj.</p><p>What does this example teach us?</p><ul><li>Objects which are created using the object literal syntax inherit properties from Object.prototype .</li></ul><h3>Summary</h3><p>So what have we seen?</p><ul><li>Objects have a private property called [[Prototype]] that points to that object’s prototype.</li><li>Almost all objects inherit properties and methods from their prototypes.</li><li>Almost all objects are descendants of Object.prototype and therefore have access to its properties and methods.</li><li>Functions have a property called prototype which is a reference to an object. That object is a descendant of the object referenced by Object.prototype.</li><li>Any object instances created by a constructor function will inherit the properties of the object at FunctionName.prototype.</li></ul><p>There are of course ways to change an object’s prototype such as Object.create or Object.setPrototypeOf, but that’s for another article. This article has only scratched the surface of prototypical inheritance, but hopefully it has helped you to understand the basic concepts.</p><p>Thanks for reading!</p><p>As promised here is the link to the code snippets used in the article: <a href="https://gist.github.com/eamon0989">https://gist.github.com/eamon0989</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f00c7af8a93c" width="1" height="1" alt=""><hr><p><a href="https://medium.com/launch-school/a-simplified-overview-of-javascript-prototypes-and-prototypical-inheritance-f00c7af8a93c">A simplified overview of JavaScript prototypes and prototypical inheritance.</a> was originally published in <a href="https://medium.com/launch-school">Launch School</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[What I’ve learned during my first month at Launch School.]]></title>
            <link>https://medium.com/launch-school/what-ive-learned-during-my-first-month-at-launch-school-6848e537db34?source=rss-43293ac2726a------2</link>
            <guid isPermaLink="false">https://medium.com/p/6848e537db34</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[how-to-study]]></category>
            <category><![CDATA[bootcamp]]></category>
            <category><![CDATA[learning-to-code]]></category>
            <category><![CDATA[launch-school]]></category>
            <dc:creator><![CDATA[Eamon O'Callaghan]]></dc:creator>
            <pubDate>Wed, 21 Jul 2021 12:13:24 GMT</pubDate>
            <atom:updated>2021-07-22T05:37:15.769Z</atom:updated>
            <content:encoded><![CDATA[<h4>What is the true value of Launch School?</h4><figure><img alt="A laptop with code open on the screen." src="https://cdn-images-1.medium.com/max/1024/1*U2doBF3eEw_mHMbeJBpuQg.jpeg" /></figure><p>First of all, a bit of context. Almost exactly 30 days ago I left my job as an English teacher to begin studying programming full-time. I chose to study at Launch School as I felt that they offered a unique approach that is in line with my way of learning. In the last 30 days, I have logged over 180 hours of focused study.</p><p>Before joining Launch school, I spent two months learning the basics of JavaScript and practicing my problem-solving skills in another well-known free online Bootcamp. In a way I’m glad I did, as it has allowed me to see more clearly the difference between what Launch School offers and what other beginner coding Bootcamps offer.</p><p>While I had learned how to solve basic problems using loops and functions and even simple recursion before coming to Launch School, Launch School focuses so much more on drilling the fundamentals of the language than on teaching you problem-solving in the beginning. For example, rather than telling you “X does this, now use it to solve these problems”, Launch School focuses on ensuring that you understand the intricacies of how X works and what the advantages and disadvantages are of using X over Y.</p><p>So what are considered to be the fundamentals?</p><p>Concepts such as:</p><ul><li>The difference between how <strong>primitives</strong> and <strong>objects</strong> are stored in memory</li><li>What <strong>truthy</strong> and <strong>falsy values</strong> are.</li><li>What <strong>variable scoping</strong> is and why it’s important.</li><li>What <strong>shadowing</strong> is and the importance of using unique, distinctive variable names.</li><li>The difference between <strong>function declarations</strong> and <strong>function expressions</strong>.</li><li>The difference between <strong>mutating vs non-mutating methods</strong>.</li><li>The difference between <strong>pass-by-reference</strong> and<strong> pass-by-value</strong> and how it affects your code.</li><li>How to go straight to the <strong>Documentation</strong> when I don’t understand something.</li><li>The importance of writing clear, <strong>easy-to-understand code</strong></li></ul><p>Some of these concepts I had already been exposed to, and I learned about them more in-depth. Others were new to me, like truthy and falsy values, mutating and non-mutating methods, and of course the all-important concept of pass-by-value and pass-by-reference. The fact that after learning these concepts, I can now analyze basic code and know exactly what’s going on and what to expect is just incredible to me.</p><p>The thing I have found most valuable though has nothing to do with the specifics of the language.</p><h3>PEDAC</h3><p>The thing I’ve found most useful so far has been PEDAC. What is PEDAC? It stands for:</p><ul><li><strong>P</strong>roblem</li><li><strong>E</strong>xamples</li><li><strong>D</strong>ata structures</li><li><strong>A</strong>lgorithm</li><li><strong>C</strong>ode with intent</li></ul><p>A big problem that I had in the beginning when trying to solve a problem was that like most Junior developers, I would just sit down and start writing code, see what worked, and gradually hack a solution together. This is very inefficient and stems from a lack of problem-solving ability combined with a lack of in-depth knowledge of the fundamentals of the language.</p><p>But after teaching you the fundamentals of JavaScript, Launch School teaches you how to approach problems systematically. First, you have to make sure you understand the problem. Take the time to read through the description well and make sure that you have understood exactly what is being asked for. I’m sure you can remember a time when you coded up a great solution to a problem, only to realize that you solved the wrong problem as you hadn’t fully understood what was being asked. So this is when you have to take the time to ask clarifying questions and get the problem straight in your mind.</p><p>Then check the examples. Is your understanding in line with the examples provided? Can you come up with some more examples? Are there any edge cases or requirements that you have missed?</p><p>What data structures will you use to represent that data while converting the input to output? (This is more used for more complex problems and is often left out when solving more simple problems).</p><p>Then you write your Algorithm. In this context, your Algorithm is a list of the steps that you will take to get the desired result. Because you have already thought about the input and required output, now you just have to decide how you are going to solve this particular problem and then put your ideas in writing.</p><p>And finally, you code up your solution. This sounds like a lot of work, and it is. It may even sound more inefficient than just sitting down and coding through the problem. And granted, for very short simple problems it can be overkill. But once the problems you are solving start to increase in complexity, PEDAC will become your new best friend.</p><p>Whenever I find myself struggling while coding a problem, it’s always because I didn’t put enough thought into my Algorithm and the steps required.</p><p>Without a strong understanding of the fundamentals, this would be impossible. Before I started studying at Launch School, I would go to StackOverflow any time I was stuck and copy a solution to a problem from there, and of course, I rarely understood what I was copying or how it worked. I can now proudly say that I haven’t needed to use StackOverflow at all this month, as I have learned how to break the problem down into smaller chunks, and when I am not 100% sure how something works, my new default is to go straight to the Documentation.</p><h4>So to sum up, what have I learned from my first 30 days in Launch School?</h4><ul><li>How to think like an engineer, to break down problems and think them through one step at a time.</li><li>The importance of knowing how JavaScript works at a fundamental level.</li><li>How to talk about code using clear, precise, and concise language.</li><li>How to read and understand documentation.</li><li>How to ask well-formed questions thus making it easier to get help if I do get stuck.</li></ul><p>All I can say is that this experience has been invaluable, and the knowledge I have gained is priceless. I can’t wait to see what’s in store for me in the next 30 days.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6848e537db34" width="1" height="1" alt=""><hr><p><a href="https://medium.com/launch-school/what-ive-learned-during-my-first-month-at-launch-school-6848e537db34">What I’ve learned during my first month at Launch School.</a> was originally published in <a href="https://medium.com/launch-school">Launch School</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>