<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: mz4</title>
    <description>The latest articles on DEV Community by mz4 (@mz4).</description>
    <link>https://dev.to/mz4</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F659807%2F994ac697-12df-4c25-bed1-91174da26d57.png</url>
      <title>DEV Community: mz4</title>
      <link>https://dev.to/mz4</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mz4"/>
    <language>en</language>
    <item>
      <title>JavaScript - Prototype</title>
      <dc:creator>mz4</dc:creator>
      <pubDate>Thu, 10 Nov 2022 09:17:33 +0000</pubDate>
      <link>https://dev.to/mz4/javascript-prototype-5dig</link>
      <guid>https://dev.to/mz4/javascript-prototype-5dig</guid>
      <description>&lt;p&gt;Every function in JavaScript has a prototype property..&lt;br&gt;
Prototype is a property that allows us to share methods across all instances of a function.&lt;br&gt;
When we create a new instance of an object, we inherit all properties and methods of that object.&lt;/p&gt;

&lt;p&gt;Turns out that this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Animal {
  constructor(name, energy) {
    this.name = name
    this.energy = energy
  }
  eat(amount) {
    this.energy += amount
  }
  sleep(length) {
    this.energy += length
  }
  play(length) {
    this.energy -= length
  }
}

const leo = new Animal('Leo', 7)
const snoop = new Animal('Snoop', 10)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;is syntactic sugar for this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function Animal (name, energy) {
  this.name = name
  this.energy = energy
}

Animal.prototype.eat = function (amount) {
  this.energy += amount
}

Animal.prototype.sleep = function (length) {
  this.energy += length
}

Animal.prototype.play = function (length) {
  this.energy -= length
}

const leo = new Animal('Leo', 7)
const snoop = new Animal('Snoop', 10)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And if we want to list all methods of an Array for example, we just have to do:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;console.log(Array.prototype);&lt;/code&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>JavaScript Engine, Heap, Call Stack, Web APIs, Event loop and Callback queue</title>
      <dc:creator>mz4</dc:creator>
      <pubDate>Sat, 05 Nov 2022 15:06:23 +0000</pubDate>
      <link>https://dev.to/mz4/javascript-engine-heap-and-call-stack-1pe6</link>
      <guid>https://dev.to/mz4/javascript-engine-heap-and-call-stack-1pe6</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--DwDZKun1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ea86sabu8adpx3gfyjv1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--DwDZKun1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ea86sabu8adpx3gfyjv1.png" alt="Javascript workflow" width="880" height="495"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;Javascript engine&lt;/strong&gt; is a program that executes JavaScript code, a popular one is Google V8 engine.&lt;/p&gt;

&lt;p&gt;It is composed by two main components the heap and callstack.&lt;br&gt;
&lt;strong&gt;The heap&lt;/strong&gt; is used for memory allocation of variables and objects, at the top of the current scope, a process called hoisting.&lt;/p&gt;

&lt;p&gt;During the phase of function execution, an execution context is created, which includes: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;a global object window (in case of the browser) &lt;/li&gt;
&lt;li&gt;a special object called 'this' and &lt;/li&gt;
&lt;li&gt;a ref to the outer environement.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A &lt;strong&gt;callstack&lt;/strong&gt; is a list that tracks functions that have been called during the execution of the code.&lt;br&gt;
JavaScript it is single threaded and it has one CallStack.&lt;br&gt;
If a function is called is pushed at the top of the call stack list, and when it is executed, it's removed from the list.&lt;/p&gt;

&lt;p&gt;Asynchronous tasks are placed in a so called &lt;strong&gt;callback queue&lt;/strong&gt; and executed only when the call stack is empty.&lt;br&gt;
The job of the &lt;strong&gt;event loop&lt;/strong&gt; is to check if the call stack is empty and in this case it executes tasks from the callback queue.&lt;br&gt;
&lt;strong&gt;Web API&lt;/strong&gt; calls are added from callstack to the web api container until an action is triggered (e.g. DOM, ajax call, button click).&lt;br&gt;
Once an action is triggered, a callback function is added to the callback queue, and executed when the call stack is empty.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://latentflip.com/loupe"&gt;interactive demo&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
