<?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:media="http://search.yahoo.com/mrss/"><channel><title><![CDATA[SiMedia Tech]]></title><description><![CDATA[Where technology emerges.]]></description><link>https://simedia.tech/blog/</link><image><url>https://simedia.tech/blog/favicon.png</url><title>SiMedia Tech</title><link>https://simedia.tech/blog/</link></image><generator>Ghost 5.81</generator><lastBuildDate>Tue, 07 Apr 2026 20:40:36 GMT</lastBuildDate><atom:link href="https://simedia.tech/blog/rss/" rel="self" type="application/rss+xml"/><ttl>60</ttl><item><title><![CDATA[JS-Quickie: pretty-printing a JSON object without external dependencies]]></title><description><![CDATA[Ever wanted to properly format a JSON string without external library? JavaScript's native JSON.stringify() provides you with exactly that functionality out of the box.]]></description><link>https://simedia.tech/blog/js-quickie-pretty-printing-a-json-object-without-external-dependencies/</link><guid isPermaLink="false">6075c238e56c8806e9f6ca64</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[Quickie]]></category><category><![CDATA[Tips]]></category><dc:creator><![CDATA[Ivan Sieder]]></dc:creator><pubDate>Thu, 15 Apr 2021 11:27:55 GMT</pubDate><media:content url="https://images.unsplash.com/photo-1555680510-34daedadbdb1?crop=entropy&amp;cs=tinysrgb&amp;fit=max&amp;fm=jpg&amp;ixid=MnwxMTc3M3wwfDF8c2VhcmNofDE1fHxjb2RlfGVufDB8fHx8MTYxODQ4NTI0Ng&amp;ixlib=rb-1.2.1&amp;q=80&amp;w=2000" medium="image"/><content:encoded><![CDATA[<img src="https://images.unsplash.com/photo-1555680510-34daedadbdb1?crop=entropy&amp;cs=tinysrgb&amp;fit=max&amp;fm=jpg&amp;ixid=MnwxMTc3M3wwfDF8c2VhcmNofDE1fHxjb2RlfGVufDB8fHx8MTYxODQ4NTI0Ng&amp;ixlib=rb-1.2.1&amp;q=80&amp;w=2000" alt="JS-Quickie: pretty-printing a JSON object without external dependencies"><p>I&apos;ve been implementing an external API interface recently and during the implementation, one task was to add a log entry to the database for the request and response payload (JSON) and display those payloads inside a user interface. Pretty straightforward.</p><p>As those JSON request/response payloads can be a few hundred lines long, it would be a mess to display them just as they are (minified and everything in one line, no spaces and no line breaks).</p><p>Of course I could have just searched for an npm package and used that one, but I usually try to install as few additional packages as possible to avoid eventual security issues for such small tasks.</p><h2 id="jsonstringify-to-the-rescue">JSON.stringify() to the rescue</h2><p>After a quick search, I came across <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify?ref=simedia.tech"><code>JSON.stringify()</code>&apos;s MDN documentation</a> and voil&#xE1;, it already provides the needed functionality out of the box, no additional library required.</p><p>Usually I&apos;m using <code>JSON.stringify()</code> to convert a JavaScript object or value to a string in order to quickly clone an object without copying just the references to that object (<a href="https://simedia.tech/blog/passing-by-value-or-by-reference-in-javascript/">read more on references in JavaScript here</a>) or to pass data along with a POST request.</p><pre><code class="language-javascript">const article = {
  title: &quot;Tesla vs. NIO&quot;,
  subtitle: &quot;Apart from the charging infrastructure, which company has the brighter future?&quot;
}

await fetch(&quot;https://api.myservice.com/articles&quot;, {
  method: &quot;POST&quot;,
  headers: {
    &quot;Content-Type&quot;: &quot;application/json&quot;
  },
  body: JSON.stringify(article)
})</code></pre><p>But as the documentation states, <code>JSON.stringify()</code> accepts up to 3 parameters. Let&apos;s see which those additional parameters are and how they can help us in properly formatting our JSON.</p><p>The second parameter is the <code>replacer</code> (used to alternate the stringification process and filter/select properties of the value, not discussed in this post right here). We&apos;ll pass along <code>null</code> for this parameter, this way all fields will be included.</p><p>Before we come to the last parameter, let&apos;s see a quick example of how <code>JSON.stringify()</code> formats a string by default:</p><pre><code class="language-javascript">console.log(JSON.stringify({ name: &quot;Ivan Sieder&quot;, address: { country: &quot;Italy&quot; } }))</code></pre><p>In our console, we&apos;ll see the following string. No formatting, indentation or whatsoever.</p><pre><code class="language-json">{&quot;name&quot;:&quot;Ivan Sieder&quot;,&quot;address&quot;:{&quot;country&quot;:&quot;Italy&quot;}}&quot;</code></pre><p>Now that we&apos;ve seen how <code>JSON.stringify()</code> works by default, let&apos;s see how the last (third) parameter can help us to properly format a JSON string. Here we can either pass a number between 0 and 10 (inclusive) or a string of up to 10 characters.</p><p>When passing a number, the JSON value will be formatted and the number will used as the amount of spaces for indentation. If 0 is being passed, the result will be the same as without this parameter. If the value is more than 10, it will be capped at 10 in any case (e.g. if you pass 14, it would be automatically set to 10). In the below example, we&apos;re passing the number 2 as the last argument, which instructs <code>JSON.stringify()</code> to format the passed value and use 2 spaces for indentation:</p><pre><code class="language-javascript">console.log(JSON.stringify({ name: &quot;Ivan Sieder&quot;, address: { country: &quot;Italy&quot; } }, null, 2));
</code></pre><p>In our console, this time we would get back the properly formatted string with indentation and line breaks:</p><pre><code class="language-json">{
  &quot;name&quot;: &quot;Ivan Sieder&quot;,
  &quot;address&quot;: {
    &quot;country&quot;: &quot;Italy&quot;
  }
}</code></pre><p>As the documentation states, the last parameter can be either a number or a string. If we pass a string instead of a number, this string (or at most the first 10 characters of it) will be used for the indentation. This could be useful for example to make the spaces better visible:</p><pre><code class="language-javascript">console.log(JSON.stringify({ name: &quot;Ivan Sieder&quot;, address: { country: &quot;Italy&quot; } }, null, &quot;&#xB7;&#xB7;&quot;));</code></pre><p>This time, our console again displays a properly formatted JSON value but instead of indenting with spaces, it uses our provided string (dots similar to the ones you might know from your editor or IDE):</p><pre><code class="language-json">{
&#xB7;&#xB7;&quot;name&quot;: &quot;Ivan Sieder&quot;,
&#xB7;&#xB7;&quot;address&quot;: {
&#xB7;&#xB7;&#xB7;&#xB7;&quot;country&quot;: &quot;Italy&quot;
&#xB7;&#xB7;}
}</code></pre><p>If you have any further questions, aspects you are unsure with or found anything that is incorrect in this article, don&apos;t hesitate to reach out to me on Twitter: <a href="https://twitter.com/ivansieder?ref=simedia.tech">@ivansieder</a></p>]]></content:encoded></item><item><title><![CDATA[Show / hide password input values with Vue.js 3 and the Composition API]]></title><description><![CDATA[Nowadays it's common to have the possibility to toggle the visibility of a password field on websites, applications, login screens etc. Implementing such a functionality with Vue.js 3 (and the Composition API) is really simple and straightforward.]]></description><link>https://simedia.tech/blog/show-hide-password-input-values-with-vue-js/</link><guid isPermaLink="false">5ae9b865a91dbb62e8e4302a</guid><category><![CDATA[Vue.js]]></category><category><![CDATA[JavaScript]]></category><dc:creator><![CDATA[Ivan Sieder]]></dc:creator><pubDate>Mon, 12 Apr 2021 17:30:00 GMT</pubDate><media:content url="https://images.unsplash.com/photo-1483193722442-5422d99849bc?ixlib=rb-0.3.5&amp;q=80&amp;fm=jpg&amp;crop=entropy&amp;cs=tinysrgb&amp;w=1080&amp;fit=max&amp;ixid=eyJhcHBfaWQiOjExNzczfQ&amp;s=5d6f5f8a48331dda37a9cd0cc45fc30e" medium="image"/><content:encoded><![CDATA[<img src="https://images.unsplash.com/photo-1483193722442-5422d99849bc?ixlib=rb-0.3.5&amp;q=80&amp;fm=jpg&amp;crop=entropy&amp;cs=tinysrgb&amp;w=1080&amp;fit=max&amp;ixid=eyJhcHBfaWQiOjExNzczfQ&amp;s=5d6f5f8a48331dda37a9cd0cc45fc30e" alt="Show / hide password input values with Vue.js 3 and the Composition API"><p>Nowadays it&apos;s common to have the possibility to toggle the visibility of a password field on websites, applications, login screens etc. Implementing such a functionality with Vue.js 3 and the Composition API is straightforward (of course, this can also be achieved with pure JavaScript. But hey, JS frameworks are trendy and fancy, so why not implement it with one?)</p><p>First off we&apos;re going to implement the example with pure JavaScript, then we&apos;ll switch over to Vue.js 3 with the classic Options API and at the end we&apos;re gonna finish off with a Vue.js 3 example which makes use of the new Composition API, script setup and <code>ref:</code> sugar.</p><p>So first of all, what will our final result look like?</p><figure class="kg-card kg-image-card"><img src="https://simedia.tech/blog/content/images/2018/02/show-hide-password-vue-2.gif" class="kg-image" alt="Show / hide password input values with Vue.js 3 and the Composition API" loading="lazy"></figure><!--kg-card-begin: markdown--><h2 id="implementationwithpurejs">Implementation with pure JS</h2>
<p>The basic idea behind showing / hiding the value of a password field is that you have a clickable element which switches the <code>type</code> attribute of the input field. That&apos;s basically all you have to do.</p>
<p>In order to understand how this works, let&apos;s first go through the states that the password input field can have:</p>
<ul>
<li><code>type</code> attribute set to <code>password</code>: this is the default state in which the input value is masqueraded</li>
<li><code>type</code> attribute set to <code>text</code>: when the input field has this state, the input value is actually visible and not masqueraded anymore</li>
</ul>
<pre><code class="language-html.line-numbers">&lt;input type=&quot;password&quot; id=&quot;password&quot;&gt;
&lt;button onclick=&quot;switchVisibility()&quot;&gt;show / hide&lt;/button&gt;

&lt;script&gt;
    const passwordField = document.querySelector(&apos;#password&apos;)

    function switchVisibility() {
      if (passwordField.getAttribute(&apos;type&apos;) === &apos;password&apos;) passwordField.setAttribute(&apos;type&apos;, &apos;text&apos;)
      else passwordField.setAttribute(&apos;type&apos;, &apos;password&apos;)
    }
&lt;/script&gt;
</code></pre>
<!--kg-card-end: markdown--><figure class="kg-card kg-embed-card"><iframe id="cp_embed_yvjzXP" src="https://codepen.io/ivansieder/embed/preview/yvjzXP?height=300&amp;slug-hash=yvjzXP&amp;default-tabs=js,result&amp;host=https://codepen.io" title="Show / hide password field value with raw JS" scrolling="no" frameborder="0" height="300" allowtransparency="true" class="cp_embed_iframe" style="width: 100%; overflow: hidden;"></iframe></figure><h4 id="what-s-going-on-here">What&apos;s going on here?</h4><ul><li><code>lines 1-2</code> declare our input field and the button, which calls the <code>switchVisibility</code> function when being clicked</li><li><code>line 5</code> gets our <code>password</code> input field into a variable so that we can edit the field&apos;s attributes later on</li><li><code>lines 7-10</code> is our implementation to handle the button click and toggle the <code>type</code> attribute value accordingly. We check if the attribute <code>type</code> of our password field is &apos;password&apos;. If it is, we change the attribute to &apos;text&apos;, so that the actual input value will be visible. If however the <code>type</code> attribute is &apos;text&apos;, we change the type to &apos;password&apos; to masquerade it&apos;s value again.</li></ul><h2 id="implementation-with-vue-js-3-options-api-">Implementation with Vue.js 3 (Options API)</h2><p>If you are familiar with the basics of Vue.js 3 (Options API), this piece of code won&apos;t scare you at all. Anyways, we&apos;ll go through each line step by step. This code produces the exactly same visual output as the pure JS implementation above.</p><figure class="kg-card kg-image-card"><img src="https://simedia.tech/blog/content/images/2021/04/carbon-1-.png" class="kg-image" alt="Show / hide password input values with Vue.js 3 and the Composition API" loading="lazy" width="1668" height="892" srcset="https://simedia.tech/blog/content/images/size/w600/2021/04/carbon-1-.png 600w, https://simedia.tech/blog/content/images/size/w1000/2021/04/carbon-1-.png 1000w, https://simedia.tech/blog/content/images/size/w1600/2021/04/carbon-1-.png 1600w, https://simedia.tech/blog/content/images/2021/04/carbon-1-.png 1668w" sizes="(min-width: 720px) 720px"></figure><figure class="kg-card kg-embed-card"><iframe id="cp_embed_PoWRPzL" src="https://codepen.io/ivansieder/embed/preview/PoWRPzL?height=300&amp;slug-hash=PoWRPzL&amp;default-tabs=js,result&amp;host=https://codepen.io" title="Show / hide password field value with Vue.js 3 (Options API)" scrolling="no" frameborder="0" height="300" allowtransparency="true" class="cp_embed_iframe" style="width: 100%; overflow: hidden;"></iframe></figure><h4 id="what-s-going-on-here-1">What&apos;s going on here?</h4><ul><li><code>lines 2-3</code> define the structure with our input and button elements</li><li><code>line 2</code> defines the input field, instructs Vue.js to keep the <code>type</code> attribute up to date with the value of the <code>passwordFieldType</code> property. Additionally, it keeps the instance&apos;s <code>password</code> property up to date with the input value and updates the input&apos;s value, if the property changes</li><li><code>line 3</code> defines our button and instructs Vue to call the method <code>switchVisibility</code> as soon as the button has been clicked</li><li><code>lines 8-13</code> define the data properties <code>password</code> and <code>passwordFieldType</code> and make them available to the template, methods, computed properties,...</li><li><code>lines 14-18</code> is again the same named function as in the pure JS example. Instead of manipulating the DOM directly, this time the method changes the value of the <code>passwordFieldType</code> property of our Vue instance. As soon as the <code>passwordFieldType</code> property gets updated, the <code>type</code> attribute of our password input field will automatically update.</li></ul><h2 id="implementation-with-vue-js-3-composition-api-">Implementation with Vue.js 3 (Composition API)</h2><p>Now that we&apos;ve seen how we can implement the show/hide functionality in pure JS and Vue.js 3 with the Options API, let&apos;s bring this example one step further and implement it with Vue.js&apos; new <a href="https://v3.vuejs.org/guide/composition-api-introduction.html?ref=simedia.tech">Composition API</a> along with <a href="https://github.com/vuejs/rfcs/pull/222?ref=simedia.tech">script setup and ref sugar</a>.</p><p>The Composition API in Vue.js 3 is totally optional, as the Option API is still there, so don&apos;t worry if you&apos;ve not heard about the Composition API yet. Feel free to first read about it and then come back to this example.</p><p>We ourselves already migrated most of our code to the new Composition API as it got a few advantages, mostly being related to scalability and code readability.</p><figure class="kg-card kg-image-card"><img src="https://simedia.tech/blog/content/images/2021/04/carbon-2-.png" class="kg-image" alt="Show / hide password input values with Vue.js 3 and the Composition API" loading="lazy" width="1956" height="558" srcset="https://simedia.tech/blog/content/images/size/w600/2021/04/carbon-2-.png 600w, https://simedia.tech/blog/content/images/size/w1000/2021/04/carbon-2-.png 1000w, https://simedia.tech/blog/content/images/size/w1600/2021/04/carbon-2-.png 1600w, https://simedia.tech/blog/content/images/2021/04/carbon-2-.png 1956w" sizes="(min-width: 720px) 720px"></figure><figure class="kg-card kg-embed-card"><iframe id="cp_embed_vYgRNjb" src="https://codepen.io/ivansieder/embed/preview/vYgRNjb?height=300&amp;slug-hash=vYgRNjb&amp;default-tabs=js,result&amp;host=https://codepen.io" title="Show / hide password field value with Vue.js 3 (Composition API)" scrolling="no" frameborder="0" height="300" allowtransparency="true" class="cp_embed_iframe" style="width: 100%; overflow: hidden;"></iframe></figure><h4 id="what-s-going-on-here-2">What&apos;s going on here?</h4><ul><li><code>lines 2-3</code> define the structure with our input and button elements</li><li><code>line 2</code> defines the input field, instructs Vue.js to keep the <code>type</code> attribute up to date with the value of the <code>passwordFieldType</code> variable. Additionally, it keeps the <code>password</code> variable up to date with the input value and updates the input&apos;s value, if the variable changes</li><li><code>line 3</code> defines our button and instructs Vue to call the method <code>switchVisibility</code> as soon as the button has been clicked</li><li><code>lines 7-8</code> define the variables <code>password</code> and <code>passwordFieldType</code> and make them available to the template, methods, computed properties,... Note that here we&apos;re making use of the new <code>ref:</code> sugar in order to keep the code concise and avoid the necessity to access refs with <code>.value</code></li><li><code>line 10</code> is again the same named function as in the above example. As soon as we click the button, this function gets called and updates the <code>passwordFieldType</code> variable. As soon as the <code>passwordFieldType</code> variable gets updated, the <code>type</code> attribute of our password input field will automatically update.</li></ul><h2 id="bonus-vue-js-login-template">Bonus - Vue.js Login Template</h2><p>The above examples are still quiet small and concise. No CSS is involved at all. If you want to play around with a slightly more sophisticated example, I&apos;ve prepared a <a href="https://codepen.io/ivansieder/pen/RwKMPOy?ref=simedia.tech">live Codepen example</a> and <a href="https://github.com/ivansieder/vuejs-login-template?ref=simedia.tech">pushed a repository to GitHub</a> which demonstrates the above mentioned techniques inside the context of a minimalistic login form.</p><p>If you have any further questions, aspects you are unsure with or found anything that is incorrect in this article, don&apos;t hesitate to reach out to me on Twitter: @ivansieder</p>]]></content:encoded></item><item><title><![CDATA[Creating a transparent base component with Vue.js]]></title><description><![CDATA[Base components in Vue.js are components, which can help you reducing the amount of work you have to do and code you have to write in order to (re)use components in Vue.js in multiple places.]]></description><link>https://simedia.tech/blog/creating-a-transparent-base-component-with-vue-js/</link><guid isPermaLink="false">5ceebd810c7a16055fc48293</guid><category><![CDATA[Vue.js]]></category><category><![CDATA[Components]]></category><category><![CDATA[reusable Components]]></category><dc:creator><![CDATA[Ivan Sieder]]></dc:creator><pubDate>Thu, 30 May 2019 06:25:30 GMT</pubDate><media:content url="https://images.unsplash.com/photo-1496814801204-280c839ea15a?ixlib=rb-1.2.1&amp;q=80&amp;fm=jpg&amp;crop=entropy&amp;cs=tinysrgb&amp;w=1080&amp;fit=max&amp;ixid=eyJhcHBfaWQiOjExNzczfQ" medium="image"/><content:encoded><![CDATA[<img src="https://images.unsplash.com/photo-1496814801204-280c839ea15a?ixlib=rb-1.2.1&amp;q=80&amp;fm=jpg&amp;crop=entropy&amp;cs=tinysrgb&amp;w=1080&amp;fit=max&amp;ixid=eyJhcHBfaWQiOjExNzczfQ" alt="Creating a transparent base component with Vue.js"><p>Base components in Vue.js are components, which can help you reducing the amount of work you have to do and code you have to write in order to (re)use components in Vue.js in multiple places. They also help you changing the markup easily, as you only have a few central places where you need to do so. An example could be a form field. You usually want to have a label along with an input field (or select, radio button, etc) but you would also like to avoid to repeat the same code over and over again for multiple input fields. One possible way of building and using such a base component is what this article is going to show you.</p><p>If you want to see the base component we&apos;re going to build in action, I&apos;ve created a CodeSanbox, which you can use to try out your ideas/use cases:</p><!--kg-card-begin: html--><a href="https://codesandbox.io/s/vuejs-base-component-8n6b5?fontsize=14&amp;ref=simedia.tech" style="box-shadow: none;" target="_blank">
  <img alt="Creating a transparent base component with Vue.js" src="https://codesandbox.io/static/img/play-codesandbox.svg">
</a>
<!--kg-card-end: html--><h2 id="basics-first">Basics first</h2><p>To build this base component, we&apos;re going to use Vue&apos;s v-model directive. To recap, here&apos;s a simple example on how to use v-model:</p><!--kg-card-begin: markdown--><pre><code class="language-javascript">export default {
  data() {
    return {
      username: &apos;&apos;
    }
  }
}
</code></pre>
<pre><code class="language-html">&lt;input type=&quot;text&quot; v-model=&quot;username&quot;&gt;
</code></pre>
<!--kg-card-end: markdown--><p>In our Vue.js instance, we have the data property <code>username</code> and in our template we bind that variable to the input field. All the v-model directive really does, is to bind the value of <code>username</code> to the <code>value</code> property of the input field (and therefor keep the content of that input field always in sync with the variable) and update the <code>username</code> variable with the new value whenever someone types something into the input field.</p><p>We can use this directive also for our base component, but we first need to understand, how v-model works under the hood. By default, v-model binds the variable you pass to the directive to the <code>value</code> property of the element/component and whenever that element/component emits an <code>input</code> event, the variable you passed to the directive will be update with the new value automatically.</p><p>This means, that the v-model directive in this example behaves equivalently to <code>v-bind:value=&quot;username&quot;</code> and <code>v-on:input=&quot;username = $event&quot;</code>. And this means, that in our base component we can expect to get a prop named <code>value</code> and emit an <code>input</code> event back to the parent with the new value when we want to update the value in the parent component.</p><h2 id="v-bind-and-v-on-shorthands">v-bind and v-on shorthands</h2><p>In the following examples we&apos;re going to use the shorthand-syntax for the v-bind and v-on directives:</p><p><code>v-bind:</code> &#xA0;becomes <code>:</code><br><code>v-on:</code> &#xA0;becomes <code>@</code></p><p>All this means is, that instead of <code>v-bind:value=&quot;username&quot;</code> we can use <code>:value=&quot;username&quot;</code> and instead of <code>v-on:input=&quot;username = $event&quot;</code> we can use <code>@input=&quot;username = $event&quot;</code>.</p><p>You can read more about this shorthand syntax in the <a href="https://vuejs.org/v2/guide/syntax.html?ref=simedia.tech#Shorthands">official Vue.js documentation</a>.</p><h2 id="building-the-base-component">Building the base component</h2><p>Now that we know how the v-model directive works under the hood, let&apos;s actually build the base component for an input field:</p><!--kg-card-begin: markdown--><pre><code class="language-javascript">export default {
  name: &quot;BaseInput&quot;,
  props: {
    label: String,
    type: String,
    placeholder: String
  }
}
</code></pre>
<pre><code class="language-html">&lt;label&gt;
  {{ label }}
  &lt;input
    :type=&quot;type&quot;
    :placeholder=&quot;placeholder&quot;
    @input=&quot;$emit(&apos;input&apos;, $event.target.value)&quot;
  &gt;
&lt;/label&gt;
</code></pre>
<!--kg-card-end: markdown--><p>In the above <code>BaseInput</code> we&apos;re expecting a prop for the label, the input type, a placeholder text and the actual value. We can use all those props inside the template and as soon as the input field emits the <code>input</code> event, we can listen to it and emit another <code>input</code> event back to the parent component in order to let the v-model directive update the variable accordingly. This way, our <code>BaseInput</code> component is a transparent component, which can be used with v-model without caring too much about how it actually works internally.</p><h2 id="using-our-baseinput-component">Using our <code>BaseInput</code> component</h2><p>Now in our parent component, we can use this <code>BaseInput</code> component in the following way (we assume that the component was registered globally and we therefor do not need to register the component locally again):</p><pre><code class="language-javascript">import BaseInput from &quot;./components/BaseInput&quot;;

export default {
  name: &quot;App&quot;,
  components: {
    BaseInput
  },
  data() {
    return {
      username: &quot;&quot;,
      password: &quot;&quot;
    };
  }
};
</code></pre><pre><code class="language-html">&lt;BaseInput label=&quot;Username&quot; type=&quot;text&quot; placeholder=&quot;Username&quot; v-model=&quot;username&quot;/&gt;
&lt;BaseInput label=&quot;Password&quot; type=&quot;password&quot; placeholder=&quot;********&quot; v-model=&quot;password&quot;/&gt;</code></pre><h2 id="further-use-cases">Further use cases</h2><p>Now this of course is just a very basic example on how you can use base components, because you could of course customize it further, build renderless components or partially renderless components, integrate slots etc.</p><p>If you have any further questions, aspects you are unsure with or found anything, that is incorrect in this article, don&apos;t hesitate to leave a comment below or reach out to me on Twitter: <a href="https://twitter.com/ivansieder?ref=simedia.tech">@ivansieder</a></p>]]></content:encoded></item><item><title><![CDATA[Passing by value or by reference? How does JavaScript handle it?]]></title><description><![CDATA[The way JavaScript works while passing around variables is something fundamental to understand if you're developing in JavaScript as it can lead to some unexpected behavior in some cases.]]></description><link>https://simedia.tech/blog/passing-by-value-or-by-reference-in-javascript/</link><guid isPermaLink="false">5b464a88e1045409af1d68cf</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[fundamentals]]></category><dc:creator><![CDATA[Ivan Sieder]]></dc:creator><pubDate>Thu, 12 Jul 2018 17:41:34 GMT</pubDate><media:content url="https://images.unsplash.com/photo-1470790376778-a9fbc86d70e2?ixlib=rb-0.3.5&amp;q=80&amp;fm=jpg&amp;crop=entropy&amp;cs=tinysrgb&amp;w=1080&amp;fit=max&amp;ixid=eyJhcHBfaWQiOjExNzczfQ&amp;s=2869da7488aeef92c5c5f2cd734479d1" medium="image"/><content:encoded><![CDATA[<img src="https://images.unsplash.com/photo-1470790376778-a9fbc86d70e2?ixlib=rb-0.3.5&amp;q=80&amp;fm=jpg&amp;crop=entropy&amp;cs=tinysrgb&amp;w=1080&amp;fit=max&amp;ixid=eyJhcHBfaWQiOjExNzczfQ&amp;s=2869da7488aeef92c5c5f2cd734479d1" alt="Passing by value or by reference? How does JavaScript handle it?"><p>The way JavaScript works while passing around variables is something fundamental to understand if you&apos;re developing in JavaScript as it can lead to some unexpected behavior in some cases.</p><p>I have posted a tweet with a poll about exactly this behavior and here I&apos;m going to dig into why &quot;2&quot; this is the output and provide some additional examples you should watch out for.</p><figure class="kg-card kg-embed-card"><blockquote class="twitter-tweet"><p lang="en" dir="ltr">&#x1F4D1; What will be the output of the following code? (RTs welcome) &#x1F914;<br>(Will post the solution tomorrow after the poll ends) &#x1F4E2;<br><br>function magic() {<br>  let a = 1;<br>  a = 2;<br>  let b = innerMagic();<br>  a = 3;<br><br>  return b;<br><br>  function innerMagic() {<br>    return a;<br>  }<br>}<br><br>console.log(magic())</p>&#x2014; Ivan Sieder (@ivansieder) <a href="https://twitter.com/ivansieder/status/1017109373569683461?ref_src=twsrc%5Etfw&amp;ref=simedia.tech">July 11, 2018</a></blockquote>
<script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>
</figure><!--kg-card-begin: markdown--><h2 id="tldr">TL;DR;</h2>
<p>In JavaScript, everything is being passed as values. The difference from primitives to objects is, that with objects you are passing a reference to an object in memory (as value) instead of passing the object directly.</p>
<h2 id="thelongerversion">The longer version</h2>
<p>Let&apos;s first get into some basic topics on how variable passing is handled for primitives vs. how it&apos;s handled for non-primitives and what hoisting in JavaScript is.</p>
<h3 id="primitives">Primitives</h3>
<p>Primitives (string, number, boolean, null, undefined, symbol) are written directly into the variable. This means, that whenever we return a primitive from a function or assign a variable (which contains a primitive) to another one, we are actually copying that variable instead of passing/saving the reference to the original one:</p>
<pre><code class="language-javascript">let a = 1;
let b = a;
a = 2;

console.log(a); // 2
console.log(b); // 1
</code></pre>
<h3 id="nonprimitives">Non-Primitives</h3>
<p>On the other hand, the non-primitive <code>object</code> is not written directly into the variable, but only the value of where that object is located in memory is placed in the variable (the pointer to the object in memory).</p>
<p>This means, that if we assign a new object literal to the variable, we are actually replacing the object with a new one (which will point to a different one in memory) and it looses reference to the original one. That&apos;s a similar behaviour as with primitive values.</p>
<pre><code class="language-javascript">let a = { name: &apos;Ivan&apos; };
let b = a;
a = { name: &apos;Not Ivan&apos; };

console.log(a); // Not Ivan
console.log(b); // Ivan
</code></pre>
<p>But if we just edit a property on the object, we are editing the original, referenced object. In the following example, the variable <code>b</code> contains the same address to an object as the variable <code>a</code>. That way, if we modify a property of the object inside the variable <code>a</code> without replacing the whole object literal, the changes will also be applied to the object, which the variable <code>b</code> points to (as both <code>a</code> and <code>b</code> are pointing to the same object in memory, as long as you don&apos;t reassign a different object).</p>
<pre><code class="language-javascript">let a = { name: &apos;Ivan&apos; };
let b = a;
a.name = &apos;Maybe Ivan?&apos;;

console.log(a); // Maybe Ivan?
console.log(b); // Maybe Ivan?
</code></pre>
<h3 id="hoisting">Hoisting</h3>
<p>Hoisting can be thought of as moving function and variable declarations to the top of your code. During compile time, your declarations will be put into memory, your code will physically still remain in the same place as you have written it. This gives us the possibility to access functions and variables in our code before they have been actually defined. You can read more about hoisting in this <a href="https://developer.mozilla.org/en-US/docs/Glossary/Hoisting?ref=simedia.tech">MDN article</a>. The article also explains greatly, why this only works for declarations and not for assignments or initializations.</p>
<pre><code class="language-javascript">log(&apos;Hi there, this works&apos;); // Hi there, this works

function log(message) {
  console.log(message)
}
</code></pre>
<h2 id="theexample">The example</h2>
<p>Now, let&apos;s dig into the example and go through it line by line:</p>
<pre><code class="language-javascript.line-numbers">function magic() {
  let a = 1;
  a = 2;
  let b = innerMagic();
  a = 3;

  return b;

  function innerMagic() {
    return a;
  }
}

console.log(magic()); // 2
</code></pre>
<p>Now let&apos;s go through how this code get&apos;s executed:</p>
<ul>
<li>Lines 1-12 define our function <code>magic</code>, which will be parsed before executing the rest of the code.</li>
<li>Inside that function, hoisting will take effect. That&apos;s why the function <code>innerMagic</code> can be used in code before the function has been declared.</li>
<li>Line 2 defines the variable <code>a</code> and assigns the value &quot;1&quot; to it. Right now, <code>a</code> equals to &quot;1&quot;.</li>
<li>Line 3 assigns the value &quot;2&quot; to the variable <code>a</code>. Right now, <code>a</code> equals to &quot;2&quot;.</li>
<li>Line 4 then calls the <code>innerMagic()</code> function (which is already available due to hoisting) and assigns the returned value to the variable <code>b</code>. As the variable <code>a</code> is set to &quot;2&quot; at the time of calling the function, <code>innerMagic()</code> will return &quot;2&quot;. Right now, <code>a</code> equals to &quot;2&quot; and <code>b</code> also equals to &quot;2&quot;.</li>
<li>Line 5 then assigns the value &quot;3&quot; to the variable <code>a</code>. And as we learned above, primitives are saved directly into the variable (not as a reference), that&apos;s why right now <code>a</code> equals to &quot;3&quot; and <code>b</code> still equals to &quot;2&quot;, as the value has been copied on line 4.</li>
<li>Line 7 lastly returns our value of the variable <code>b</code>, which is still set to &quot;2&quot;.</li>
</ul>
<h2 id="objectexample">Object example</h2>
<p>Now we have seen the example with primitives, let&apos;s rewrite it to use objects and show, where pitfalls can occur:</p>
<pre><code class="language-javascript.line-numbers">function magic() {
  let a = { name: &apos;Ivan&apos; };
  a.name = &apos;Julian&apos;;
  let b = innerMagic();
  a.name = &apos;Kilian&apos;;

  return b;

  function innerMagic() {
    return a;
  }
}

console.log(magic()); // { name: &apos;Kilian&apos; }
</code></pre>
<p>Now let&apos;s go through how this code get&apos;s executed:</p>
<ul>
<li>Lines 1-12 define our function <code>magic</code>, which will be parsed before executing the rest of the code.</li>
<li>Inside that function, hoisting will take effect. That&apos;s why the function <code>innerMagic</code> can be used in code before the function has been declared.</li>
<li>Line 2 defines the variable <code>a</code> and assigns the object &quot;{ name: &apos;Ivan&apos; }&quot; to it. Right now, <code>a</code> equals to &quot;{ name: &apos;Ivan&apos; }&quot;.</li>
<li>Line 3 changes the <code>name</code> property of the variable <code>a</code> to &quot;Julian&quot;. Right now, <code>a</code> equals to &quot;{ name: &apos;Julian&apos; }&quot;.</li>
<li>Line 4 then calls the <code>innerMagic()</code> function (which is already available due to hoisting) and assigns the returned value to the variable <code>b</code>. As the variable <code>a</code> is an object at the time of calling the function, <code>innerMagic()</code> will return the address to the object which <code>a</code> points to. Right now, <code>a</code> equals to &quot;{ name: &apos;Julian&apos; }&quot; and <code>b</code> also equals to &quot;{ name: &apos;Julian&apos; }&quot;. They both point to the same object in memory.</li>
<li>Line 5 then changes the property <code>name</code> of the variable <code>a</code> to &quot;Kilian&quot;. And as we learned above, objects are not saved directly into the variable, but instead the address to the object in memory is saved, that&apos;s why right now <code>a</code> equals to &quot;{ name: &apos;Kilian&apos; }&quot; and <code>b</code> also equals to &quot;{ name: &apos;Kilian&apos; }&quot;, as both variables still point to the same object in memory and we&apos;ve just modified the actual object in memory and not the content of the variable itself. The address still remains the same in both variables.</li>
<li>Line 7 lastly returns our value of the variable <code>b</code>, which is still set to &quot;{ name: &apos;Kilian&apos; }&quot;.</li>
</ul>
<p>If you have any further questions, aspects you are unsure with or found anything, that is incorrect in this article, don&apos;t hesitate to leave a comment below or reach out to me on Twitter: <a href="https://twitter.com/ivansieder?ref=simedia.tech">@ivansieder</a></p>
<!--kg-card-end: markdown-->]]></content:encoded></item><item><title><![CDATA[Delivering dynamic non-HTTPS content over HTTPS with the help of AWS]]></title><description><![CDATA[HTTPS is not only important for security, but also for SEO. In this article you'll get an idea how you can deliver non-HTTPS content over HTTPS with AWS.]]></description><link>https://simedia.tech/blog/dynamic-http-content-https-aws/</link><guid isPermaLink="false">5ae9b865a91dbb62e8e43025</guid><category><![CDATA[AWS]]></category><category><![CDATA[Lambda]]></category><category><![CDATA[CloudWatch]]></category><category><![CDATA[S3]]></category><category><![CDATA[CloudFront]]></category><category><![CDATA[HTTPS]]></category><category><![CDATA[TLS]]></category><category><![CDATA[SSL]]></category><dc:creator><![CDATA[Ivan Sieder]]></dc:creator><pubDate>Mon, 17 Jul 2017 14:35:00 GMT</pubDate><media:content url="https://images.unsplash.com/photo-1481966115753-963394378f23?ixlib=rb-0.3.5&amp;q=80&amp;fm=jpg&amp;crop=entropy&amp;cs=tinysrgb&amp;w=1080&amp;fit=max&amp;ixid=eyJhcHBfaWQiOjExNzczfQ&amp;s=677db8f21f036f8681dfed4b468ad3d3" medium="image"/><content:encoded><![CDATA[<!--kg-card-begin: markdown--><img src="https://images.unsplash.com/photo-1481966115753-963394378f23?ixlib=rb-0.3.5&amp;q=80&amp;fm=jpg&amp;crop=entropy&amp;cs=tinysrgb&amp;w=1080&amp;fit=max&amp;ixid=eyJhcHBfaWQiOjExNzczfQ&amp;s=677db8f21f036f8681dfed4b468ad3d3" alt="Delivering dynamic non-HTTPS content over HTTPS with the help of AWS"><p>Having a HTTPS-encrypted website is becoming more and more important. In fact, by now it should already be the standard and we should not even have to talk about this topic anymore.</p>
<p>As we started migrating our customers&apos; websites to HTTPS, we faced the problem, that webcams most of the time weren&apos;t hosted on HTTPS-enabled domains and/or their providers had no plans to migrate them to HTTPS anytime soon.</p>
<p>But our customers of course wanted to have them on their websites. So we had to work out a solution, which could be realized fast and doesn&apos;t cause any maintenance costs. There are of course a few different ways to do this. But I&apos;ll explain the one we found the most useful in our case.</p>
<h3 id="tldr">TL;DR</h3>
<p>We have an <strong>AWS Lambda function</strong>, which fetches images from URLs stored inside an array and saves them to a <strong>S3 Bucket</strong>. This Lambda function is triggered every 10 minutes by a <strong>CloudWatch Event</strong>. To allow us to serve the images from a custom domain (with HTTPS-support), we set up <strong>CloudFront</strong> with TTL set to 0 (more on this below).</p>
<h3 id="awslambda">AWS Lambda</h3>
<p>Our AWS Lambda function has the sole purpose of fetching all images from a URL and store them to an S3 bucket with a provided file name (to avoid duplicate names). For this purpose, we use Node.js&apos; internal <code>http.get()</code> function.</p>
<p>To save the image from Node.js to an S3 bucket, the best and most stable way was to use the AWS SDK (<a href="https://aws.amazon.com/documentation/sdk-for-javascript/?ref=simedia.tech">https://aws.amazon.com/documentation/sdk-for-javascript/</a>)</p>
<p>Additionally to just fetching the image, we also do some pre-checks, if the URL is being redirected and follow those redirects to make sure we actually fetch the real image instead of the redirection page.</p>
<h3 id="awscloudwatch">AWS CloudWatch</h3>
<p>AWS CloudWatch Event rules allow us to trigger a Lambda function every 10 minutes, so we regularly get fresh images from the origin servers.</p>
<h3 id="s3bucket">S3 Bucket</h3>
<p>AWS S3 (Simple Storage Service) provides us with persistent and replicated storage for all our webcam images and simplifies the process of adding HTTPS to our webcams. It also keeps us distant from managing storage permissions etc.</p>
<h3 id="cloudfront">CloudFront</h3>
<p>You might wonder, why we had to set up CloudFront additionally to the S3 bucket. At the time of writing it was not possible to have a S3 bucket with a custom domain and a valid TLS-certificate, that&apos;s why we needed to set up CloudFront additionally to the S3 bucket.</p>
<p>Now there&apos;s usually a problem with CDNs when delivering dynamic content, as it&apos;s primary purpose is to deliver static content which doesn&apos;t change too frequently. But luckily, CloudFront has an option for this purpose.</p>
<p>AWS CloudFront can be set up with a TTL of 0 seconds, which means, that CloudFront will still cache the content, but it will make a GET request with an If-Modified-Since header to allow the origin to tell CloudFront, if the image can still be served from the cache or if it has to be pulled from the origin.</p>
<h3 id="conclusion">Conclusion</h3>
<p>Using cloud services for such simple services can really save up time in terms of maintenance and management, as you don&apos;t have to care about OS updates, public-facing storage, distribution</p>
<h6 id="usefulresources">Useful resources</h6>
<p>Here&apos;s a list with a few resources you might check out to learn more about Amazon Web Services (AWS):</p>
<ul>
<li>AWS Lambda - <a href="https://aws.amazon.com/lambda/?ref=simedia.tech">https://aws.amazon.com/lambda/</a></li>
<li>AWS CloudFront - <a href="https://aws.amazon.com/cloudfront/?ref=simedia.tech">https://aws.amazon.com/cloudfront/</a></li>
<li>AWS S3 - <a href="https://aws.amazon.com/s3/?ref=simedia.tech">https://aws.amazon.com/s3/</a></li>
<li>AWS CloudWatch - <a href="https://aws.amazon.com/cloudwatch/?ref=simedia.tech">https://aws.amazon.com/cloudwatch/</a></li>
</ul>
<p>If you have any further questions, aspects you are unsure with or found anything, that is incorrect in this article, don&apos;t hesitate to leave a comment below or reach out to me on Twitter: <a href="https://twitter.com/ivansieder?ref=simedia.tech">@ivansieder</a>.</p>
<!--kg-card-end: markdown-->]]></content:encoded></item><item><title><![CDATA[JavaScript prototypes. It doesn't have to be complicated.]]></title><description><![CDATA[Understanding the concept of prototypes in JavaScript is essential, if you want to dive deeper into JS. This post gives you a simple explanation.]]></description><link>https://simedia.tech/blog/javascript-prototypes-it-does-not-have-to-be-complicated/</link><guid isPermaLink="false">5ae9b865a91dbb62e8e43024</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[Prototypes]]></category><category><![CDATA[Prototypal inheritance]]></category><category><![CDATA[Prototype chain]]></category><dc:creator><![CDATA[Ivan Sieder]]></dc:creator><pubDate>Wed, 31 May 2017 14:00:00 GMT</pubDate><media:content url="https://images.unsplash.com/photo-1452860606245-08befc0ff44b?ixlib=rb-0.3.5&amp;q=80&amp;fm=jpg&amp;crop=entropy&amp;cs=tinysrgb&amp;w=1080&amp;fit=max&amp;ixid=eyJhcHBfaWQiOjExNzczfQ&amp;s=304f7f4af519a870bdf74f4077f44f60" medium="image"/><content:encoded><![CDATA[<!--kg-card-begin: markdown--><img src="https://images.unsplash.com/photo-1452860606245-08befc0ff44b?ixlib=rb-0.3.5&amp;q=80&amp;fm=jpg&amp;crop=entropy&amp;cs=tinysrgb&amp;w=1080&amp;fit=max&amp;ixid=eyJhcHBfaWQiOjExNzczfQ&amp;s=304f7f4af519a870bdf74f4077f44f60" alt="JavaScript prototypes. It doesn&apos;t have to be complicated."><p>It&apos;s hard to skip <strong>prototypes</strong> (or the <strong>prototype chain</strong> or <strong>prototypal inheritance</strong>) if you want to dive deeper into JS development (Not only pure JS, but also Node.js, Frameworks like Vue.js, React.js, Angular,...).</p>
<p>If you are like me a few years ago and get some serious headache each time you have to deal with prototypes in JavaScript, this will probably be <strong>THE</strong> article you were looking for to get your head wrapped around the principle of prototypes in JS.</p>
<p>From the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Inheritance_and_the_prototype_chain?ref=simedia.tech">MDN documentation</a> (don&apos;t be scared, we&apos;ll clarify everything below):</p>
<blockquote>
<p>When it comes to inheritance, JavaScript only has one construct: objects. Each object has a private property (referred to as [[Prototype]]) which holds a link to another object called its prototype. That prototype object has a prototype of its own, and so on until an object is reached with null as its prototype. By definition, null has no prototype, and acts as the final link in this prototype chain.</p>
</blockquote>
<h2 id="privateprototypeproperty">Private <code>[[Prototype]]</code> property</h2>
<p>Refer back to the below illustration whenever you feel the need, it might help you to wrap your head around this concept easier.<br>
<img src="https://simedia.tech/blog/content/images/2018/01/prototype-chain.jpg" alt="JavaScript prototypes. It doesn&apos;t have to be complicated." loading="lazy"></p>
<p>So let&apos;s start from the base. Every object in JavaScript has a private <code>[[Prototype]]</code> prototype, which holds a reference to another object (or <code>null</code>). This means, that our new object will have access to methods and properties of that referenced object.</p>
<p>The private prototype object can be accessed with <code>Object.getPrototypeOf()</code> (to get the private prototype property) and <code>Object.setPrototypeOf()</code> (to set the private prototype property). Additionally you can also use <code>__proto__</code> in some browsers to get and set this prototype.</p>
<p>In my opinion, things are easier with examples. So let&apos;s take a string to demonstrate how the prototype chain works.</p>
<pre><code class="language-javascript.line-numbers">const name = &apos;SiMedia&apos;;
</code></pre>
<p>Let&apos;s verify the prototype chain of the above string with the following code:</p>
<pre><code class="language-javascript.line-numbers">console.log(name.__proto__); // String {}
console.log(name.__proto__.__proto__); // Object {}
console.log(name.__proto__.__proto__.__proto__); // null
</code></pre>
<p>As we can see, our variable <code>name</code> has it&apos;s private prototype property set to <code>String</code>, so we can use methods like <code>name.charAt()</code> without the need to define them on our <code>name</code> object directly.</p>
<p>In the above example, the <code>String.prototype</code> object also has a private prototype set to <code>Object.prototype</code> (this happens, because <code>String.prototype</code> already inherits by default from <code>Object.prototype</code>. This allows us to use methods like <code>name.watch()</code>, even if the method isn&apos;t defined on the <code>name</code> object nor on the <code>String</code> object.</p>
<p>How does the prototype chain actually work? Let&apos;s imagine you call <code>name.charAt()</code>. First, JavaScript will search in the <code>name</code> object for a property named <code>charAt</code>. If there is no such property, JS will walk up the prototype chain. In our case, it will reach the <code>String.prototype</code> object and look, if there is a property <code>charAt</code>. Woohoo, we got such a property <code>charAt</code> on <code>String</code>. So the method <code>charAt</code> from the <code>String</code> object will be called on our <code>name</code> object.</p>
<p>This prototype chain could go up indefinitely, until the chain reaches a private prototype property set to <code>null</code> (as it is the case for <code>Object.prototype</code>).</p>
<p>Now we know, that each object has a private <code>[[Prototype]]</code> object, which references another object (or <code>null</code>) and allows us to inherit methods and properties almost indefinitely.</p>
<h2 id="prototypeproperty"><code>prototype</code> property</h2>
<p>But that&apos;s not everything you need to know about prototypes in JavaScript</p>
<p>Whenever we define a function, this function automatically gets assigned a property <code>prototype</code>, which by default will be set to an object containing two properties. <code>constructor</code>, which holds a reference to the function itself and <code>__proto__</code> which references the private <code>[[Prototype]]</code> object for this object (or <code>null</code>). We can verify this with the following code (You can test this in your browser console or in a Node.js app).</p>
<pre><code class="language-javascript.line-numbers">// Just an empty function, to keep things simple.
function Car() {}

console.log(Car.prototype.constructor); // function Car()
console.log(Car.prototype.__proto__); // Object {}
</code></pre>
<p>So what is this <code>prototype</code> object?</p>
<p>The above function can be used (as every function) to construct a new object. If we create a new instance by using this function, the private <code>[[Prototype]]</code> property of that new instance will be set to reference the <code>prototype</code> property of the constructor function.</p>
<pre><code class="language-javascript.line-numbers">const tesla = new Car();

console.log(tesla.__proto__.constructor); // function Car()
console.log(tesla.__proto__ === Car.prototype); // true
</code></pre>
<p>As you can see, the private <code>[[Prototype]]</code> property of <code>tesla</code> is the same as the <code>prototype</code> property of the <code>Car</code> constructor function, because by calling <code>new Car()</code>, the <code>tesla</code> object gets it&apos;s private <code>[[Prototype]]</code> set to the <code>Car.prototype</code> object.</p>
<p>It&apos;s considered good practise, to add methods and static properties to this prototype object, as this way the methods won&apos;t be added to each instance separately, but instead they will be created only once and can be shared across multiple objects. This can save a lot of memory, depending on how many instances and methods youhave.</p>
<h2 id="inheritance">Inheritance</h2>
<p>The prototype chain, as you might have already guessed, is being used to inherit methods and properties from other objects. But how do you actually set up the inheritance?</p>
<p>As we have learned above, we can set the private <code>[[Prototype]]</code> property of an object by calling <code>Object.setPrototypeOf()</code> or by using the <code>__proto__</code> property.</p>
<p>Again, examples might make it more obvious. Let&apos;s imagine, we have a manually created tesla object, but we want it to inherit some methods from <code>Car</code>:</p>
<pre><code class="language-javascript.line-numbers">function Car() {}

Car.prototype.drive = function() {
  console.log(&apos;The car is driving...&apos;);
};

function Tesla () {}

Object.setPrototypeOf(Tesla.prototype, Car.prototype);

const tesla = new Tesla();

tesla.drive(); // The car is driving...
</code></pre>
<p>Let&apos;s go through the above code line by line:</p>
<ul>
<li>In line 1 we declare our <code>Car</code> constructor function. It won&apos;t do any assignments, to keep things simple.</li>
<li>Line 3-5 adds the function <code>drive</code> to the <code>Car</code> prototype object, which logs to the console.</li>
<li>Line 7 defines our <code>Tesla</code> constructor function. Again, we won&apos;t do any assignment, to keep things simple.</li>
<li>Line 9 is where the magic happens. With the <code>Object.setPrototypeOf()</code> method, we tell JavaScript to set the private <code>[[Prototype]]</code> property of the <code>Tesla.prototype</code> object to reference the <code>Car.prototype</code> object. Now we can use methods and properties declared on the <code>prototype</code> object of <code>Car</code></li>
<li>Line 11 creates a new object from the <code>Tesla</code> function constructor. Remember, that we set the private <code>[[Prototype]]</code> of <code>Tesla.prototype</code> to reference <code>Car.prototype</code>. And when instantiating, the <code>Tesla.prototype</code> will be added as the private <code>[[Prototype]]</code> to our new created object. This way, we will have access to all functions and properties, which were attached to <code>Tesla.prototype</code> and <code>Car.prototype</code>.</li>
<li>Line 13 calls the <code>drive</code> method on the <code>tesla</code> object. As the <code>Tesla</code> constructor function itself doesn&apos;t have this method, JavaScript walks up the prototype chain and finds the method on the <code>Car.prototype</code> object. The output will then be &apos;The car is driving...&apos;</li>
</ul>
<p>So, for the above example, the prototype chain will look like this (simplified):</p>
<pre><code>tesla.__proto__ -&gt; Tesla.prototype
Tesla.prototype.__proto__ -&gt; Car.prototype
Car.prototype.__proto__ -&gt; Object.prototype
Object.prototype.__proto__ -&gt; null
</code></pre>
<h2 id="bonusshadowing">Bonus: shadowing</h2>
<p>When inheriting, it&apos;s often required, to override a method or property, which came from an inherited class (or constructor function). In prototypal inheritance, this is called shadowing.</p>
<p>Let&apos;s imagine for a second, that we don&apos;t want our <code>tesla</code> object to only drive, but instead flowing smoooth on the street. Maybe we use a 3rd party library, which provides us the <code>Car</code> base class and it&apos;s good practice, not to override 3rd party prototypes. So the only way to replace/extend our <code>drive</code> method from <code>Car</code>, is to add a method <code>drive</code> to the <code>Tesla</code> prototype object.</p>
<p>Let&apos;s get our hands dirty:</p>
<pre><code class="language-javascript.line-numbers">function Car() {}

Car.prototype.drive = function() {
  console.log(&apos;The car is driving...&apos;);
};

function Tesla () {}

Object.setPrototypeOf(Tesla.prototype, Car.prototype);

Tesla.prototype.drive = function() {
  console.log(&apos;The tesla is flowing on the street smoothly.&apos;);
};

const tesla = new Tesla();

tesla.drive(); // The tesla is flowing on the street smoothly.
</code></pre>
<p>Notice, it&apos;s important, that you call <code>Object.setPrototypeOf()</code> before adding new methods to the <code>Tesla.prototype</code> object, as they will be overridden otherwise.</p>
<p>In the above code, as soon as we call <code>tesla.drive()</code>, JavaScript again starts at the base of the prototype chain, in our case the <code>tesla</code> object itself. The <code>tesla</code> object itself doesn&apos;t have such a method, so JavaScript goes up to it&apos;s prototype. The <code>Tesla.prototype</code> object has a function named <code>drive</code> which will be called. At this point, there&apos;s no need to walk up the prototype chain anymore. That&apos;s why the <code>drive</code> method of <code>Car.prototype</code> won&apos;t be called.</p>
<p>But what, if we want to call both methods? Add the following snippet to the <code>Tesla.prototype.drive</code> method, wherever you want the code from the inherited class to be executed: <code>Car.prototype.drive.call(this)</code>. This will call the <code>drive</code> method of <code>Car.prototype</code> and bind it&apos;s this keyword to the object you are currently interacting with.</p>
<pre><code class="language-javascript.line-numbers">function Car() {}

Car.prototype.drive = function() {
  console.log(&apos;The car is driving...&apos;);
};

function Tesla () {}

Object.setPrototypeOf(Tesla.prototype, Car.prototype);

Tesla.prototype.drive = function() {
  Car.prototype.drive.call(this);
  console.log(&apos;The tesla is flowing on the street smoothly.&apos;);
};

const tesla = new Tesla();

tesla.drive();
// The car is driving...
// The tesla is flowing on the street smoothly.
</code></pre>
<h2 id="conclusion">Conclusion</h2>
<p>We saw, that each object has a private <code>[[Prototype]]</code> property, which defines, which object we are inheriting properties and methods from. We also saw, that functions have a <code>prototype</code> property, which is an object, that gets set as the private <code>[[Prototype]]</code> property of instances created with that function.</p>
<p>There is a lot more you can learn about prototype. If you understood the above principles, I recommend you red through the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Inheritance_and_the_prototype_chain?ref=simedia.tech">MDN Documentation</a> and get your hands dirty by experimenting.</p>
<p>If you have any further questions, aspects you are unsure with or found anything, that is incorrect in this article, don&apos;t hesitate to reach out to me on Twitter: <a href="https://twitter.com/ivansieder?ref=simedia.tech">@ivansieder</a>.</p>
<!--kg-card-end: markdown-->]]></content:encoded></item><item><title><![CDATA[Vue.js quick tip: Using destructuring assignment in for loops]]></title><description><![CDATA[Using destructuring assignment in Vue.js for-loops can make the codebase look cleaner, easier to read and more concise.]]></description><link>https://simedia.tech/blog/vue-js-destructuring-assignment-for-loops/</link><guid isPermaLink="false">5ae9b865a91dbb62e8e43020</guid><category><![CDATA[Vue.js]]></category><category><![CDATA[ES6]]></category><dc:creator><![CDATA[Ivan Sieder]]></dc:creator><pubDate>Thu, 11 May 2017 15:26:50 GMT</pubDate><media:content url="https://images.unsplash.com/photo-1504308805006-0f7a5f1f0f71?ixlib=rb-0.3.5&amp;q=80&amp;fm=jpg&amp;crop=entropy&amp;cs=tinysrgb&amp;w=1080&amp;fit=max&amp;ixid=eyJhcHBfaWQiOjExNzczfQ&amp;s=22778481547291ad5240eac904735af9" medium="image"/><content:encoded><![CDATA[<!--kg-card-begin: markdown--><blockquote>
<img src="https://images.unsplash.com/photo-1504308805006-0f7a5f1f0f71?ixlib=rb-0.3.5&amp;q=80&amp;fm=jpg&amp;crop=entropy&amp;cs=tinysrgb&amp;w=1080&amp;fit=max&amp;ixid=eyJhcHBfaWQiOjExNzczfQ&amp;s=22778481547291ad5240eac904735af9" alt="Vue.js quick tip: Using destructuring assignment in for&#xA0;loops"><p><a href="https://vuejs.org/?ref=simedia.tech">Vue.js</a> is a modern, lightweight frontend framework which massively gained popularity over the past few years. If you have never heard about Vue.js before and are interested in learning more about it, the best way to get started is to read through the <a href="https://vuejs.org/guide/?ref=simedia.tech">official guide</a>.</p>
</blockquote>
<p>Vue.js provides a <code>v-for</code> directive, which can be used for displaying a list of items using the data from an array.</p>
<p>The following example renders a list of products with it&apos;s name and price.</p>
<pre><code class="language-javascript">var app = new Vue({
  el: &apos;#app&apos;,
  data: {
    products: [
      { name: &apos;Product 1&apos;, price: 300 },
      { name: &apos;Product 2&apos;, price: 100 },
      { name: &apos;Product 3&apos;, price: 200 }
    ]
  }
});
</code></pre>
<pre><code class="language-html">&lt;ul&gt;
  &lt;li v-for=&quot;product in products&quot;&gt;{{ product.name }} - {{ product.price }}&lt;/li&gt;
&lt;/ul&gt;
</code></pre>
<p><a href="https://codepen.io/ivansieder/pen/EmEZGj?editors=1010&amp;ref=simedia.tech">Edit on CodePen</a></p>
<p>This is pretty straight forward and the way you would usually want to render a list.</p>
<p>There is really nothing wrong with the above code, but there is a small aspect, which really bugs me. As you can see, we are repeating the <code>product</code> identifier inside the output twice (<code>{{ product.name }}</code> and <code>{{ product.price }}</code>).</p>
<p>We are modern web developers (at least I hope so) and therefore we are going to use modern ES6 functionality (ignoring the fact, that old browsers, which almost nobody uses, don&apos;t support those features - namely IE).</p>
<p>The feature we are going to use is called <a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment?ref=simedia.tech">destructuring assignment</a>. Basically, what the destructuring assignment allows us to do, is to pull a specific property off of an object and store it in a variable.</p>
<p>Now, rewriting the above code with the destructuring assignment, we can refactor the rendering part to the following:</p>
<pre><code class="language-html">&lt;ul&gt;
  &lt;li v-for=&quot;{name, price} in products&quot;&gt;{{ name }} - {{ price }}&lt;/li&gt;
&lt;/ul&gt;
</code></pre>
<p><a href="https://codepen.io/ivansieder/pen/QvmpyP?editors=1010&amp;ref=simedia.tech">Edit on CodePen</a></p>
<p>So you might wonder: What does the above code now do differently than before? Well, the output will still be the same, but in my opinion this approach is way more readable and easier to understand. The above code pulls off the properties named <code>name</code> and <code>price</code> from each element inside the products array and stores them in variables named <code>name</code> and <code>price</code>. That way, inside the for loop you can use the variables <code>name</code> and <code>price</code> instead of referencing them with <code>product.name</code> and <code>product.price</code> each time.</p>
<p>This might not seem too useful in the above, short example. But imagine you have a much longer handler inside the for loop, in which you need to reference multiple properties multiple times. In that case, you might save a few key strokes, as you don&apos;t have to reference them with the <code>product</code> identifier each time.</p>
<!--kg-card-end: markdown-->]]></content:encoded></item></channel></rss>