<?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>Forem: Alireza Hamid</title>
    <description>The latest articles on Forem by Alireza Hamid (@alirezahamid).</description>
    <link>https://forem.com/alirezahamid</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%2F441594%2F6b205d88-4431-44e6-a02d-4d8f0f6e09a5.jpg</url>
      <title>Forem: Alireza Hamid</title>
      <link>https://forem.com/alirezahamid</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/alirezahamid"/>
    <language>en</language>
    <item>
      <title>JavaScript Engine, a true story (Part 1)</title>
      <dc:creator>Alireza Hamid</dc:creator>
      <pubDate>Mon, 29 Aug 2022 19:15:29 +0000</pubDate>
      <link>https://forem.com/alirezahamid/javascript-engine-a-true-story-part-1-1hp1</link>
      <guid>https://forem.com/alirezahamid/javascript-engine-a-true-story-part-1-1hp1</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--650WpcT1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/exaelo81o7u9rf7t9rl9.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--650WpcT1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/exaelo81o7u9rf7t9rl9.jpeg" alt="Image description" width="880" height="586"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Anyone who has done any work with JavaScript is likely familiar with the V8 engine. Most people are aware that JavaScript operates in a single thread and employs a callback, which is crucial to the idea behind the language’s engine. Another common misconception is that JavaScript is a compiled language.&lt;/p&gt;

&lt;p&gt;Let’s tackle what that means over the next couple of articles.&lt;/p&gt;

&lt;h2&gt;
  
  
  The mysterious “Engine” about which we speak
&lt;/h2&gt;

&lt;p&gt;Well, if I write some code like this:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;br&gt;
const iAmHappy = true;&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Just some JavaScript that we whipped up, really. Currently, I am setting the boolean &lt;code&gt;iAmHappy&lt;/code&gt; to its correct value. What does this mean, exactly, and how does the computer interpret it? Picture this: someone hands you a computer and instructs you to show a picture of flowers on the screen in Chinese.&lt;/p&gt;

&lt;p&gt;Is the computer going to understand what you’re saying? No.&lt;br&gt;
What you just said would be completely lost on the computer. Similarly, if I gave you a computer with a CPU. A file that contains JavaScript. And I tell it, hey, read this file and help me out. At the end of the day, the computer just knows ones and zeros. And when we give it the JavaScript file, it’s like me speaking Chinese to a computer and the computer saying, What? What are you on about?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--tJCT66oe--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yspk02zswbd65csdkjgj.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--tJCT66oe--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yspk02zswbd65csdkjgj.jpeg" alt="Image description" width="880" height="417"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Plus, speaking to a computer in Chinese while people stare at you as though you’re crazy. As a result, the machine has no idea what JavaScript is. So, how can we communicate using a JavaScript file so that the computer displays floral images? The JavaScript engine is the initial stage in our learning process. With a JavaScript engine right here, you can give this machine the JavaScript file, and this machine will comprehend it and instruct the computer on what to do to display photos of flowers.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://en.wikipedia.org/wiki/List_of_ECMAScript_engines"&gt;Here is the list of JavaScript engines&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Google-made Engine — V8 Engine
&lt;/h2&gt;

&lt;p&gt;So, whenever we utilise an engine, we can pass it a JavaScript file. And the engine understands this JavaScript file, allowing it to communicate with the machine, the computer, and instruct it to do what we ask it to do with JavaScript. These engines are now created by programmers.&lt;/p&gt;

&lt;p&gt;The V8 engine, for example, is written in C++. But why do these engines become created? When it comes to JavaScript, 2008 was a watershed event in history because Google published V8. Previously, most browsers used extremely primitive engines, which meant that JavaScript was a little slow. Google, you see, had this issue. They had a programme called Google Maps.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--RPurYnRh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yxf0bl68wu2jcblai834.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--RPurYnRh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yxf0bl68wu2jcblai834.jpeg" alt="Image description" width="600" height="600"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you may be aware, Google Maps is rather a power hog. Multiple functions are supported by it. Google Maps is quite slow in the browser because of features like asking for directions, zooming in and out, and possibly even using Street View. And Google, being a search engine, would prefer that everyone use their search engine, which is why they developed a browser: to increase their market share.&lt;/p&gt;

&lt;p&gt;So they used Google Maps and their own Chrome browser and thought, Hmm. The V8 engine will be our own JavaScript engine that we will create. As a result, JavaScript now executes much faster in the browser than it did previously. They also released V eight in 2008. But the main takeaway for us here is that extremely smart people work on these engines to ensure that our JavaScript executes as quickly as possible in the browser, on the server, or on any type of computer. As a result of the work that goes into these engines, JavaScript becomes faster and faster for us every day.&lt;/p&gt;

&lt;p&gt;But what is inside this miraculous machine that knows JavaScript? It reads our code and then executes it. :)&lt;/p&gt;

&lt;p&gt;Here’s a fun question. Who do you think created the very first JavaScript engine?&lt;/p&gt;

&lt;p&gt;Brendan Eich, the creator of the first JavaScript engine, was the first to create it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--L_0Wzp7N--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zu81a7wmj4nyu5uuddzh.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--L_0Wzp7N--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zu81a7wmj4nyu5uuddzh.jpg" alt="Image description" width="880" height="880"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;While working at Netscape, the first commercially available browser, this individual invented the early version of what became known as Spider Monkey, which Firefox still uses as their JavaScript engine today. And Brendan Eich was the first to invent the language JavaScript to implement this engine, allowing you to execute JavaScript on a browser that could previously only read HTML and CSS.&lt;/p&gt;

&lt;p&gt;So we know that the JavaScript engine accepts our written JavaScript code and performs some magic to instruct the machine to do what we want. So, what’s going on within this engine? And now comes the hard part.&lt;/p&gt;

&lt;p&gt;As we all know, anyone can build this engine. Yes, you can create your own JavaScript engine, but it’s a lot of work. And, at the end of the day, it’s just a computer programme. And the V8 engine, which is the most popular, most common, and, according to some, quickest JavaScript engine used by the Chrome browser and Node.js, which we’ll discuss later. C++, a low-level programming language, is used to create this engine. But first, let’s take a look at what’s going on inside.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Nz25bxOd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o9uccp1ia2xvsfyrlhjv.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Nz25bxOd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o9uccp1ia2xvsfyrlhjv.jpeg" alt="Image description" width="880" height="455"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We provide it with a JavaScript file.&lt;/p&gt;

&lt;p&gt;And initially, as does lexical analysis, which splits the code into tokens to identify their meaning so that we know what the code is attempting to achieve. And these tokens are combined to produce an AST, which stands for Abstract Syntax Tree.&lt;/p&gt;

&lt;p&gt;So we parse the code, that is, we try to figure out how the text is divided up based on JavaScript keywords, and it forms this tree-like structure called abstract syntax tree.&lt;/p&gt;

&lt;p&gt;And here’s a nice little web tool you can use to demonstrate this. It depicts the appearance of this &lt;a href="https://astexplorer.net/"&gt;abstract syntax tree&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;And, while this may appear to you as gibberish right now, it helps the engine to understand what’s going on in the programme or, at the very least, break things down one by one. And once this form is completed, it passes through a process known as an interpretation. We use the Profiler compiler to generate some code, which we will discuss later. And this entire engine will spew out some code that our CPUs on our computers will comprehend in order to give it instructions.&lt;/p&gt;

&lt;p&gt;What problem(s) do you see with everyone designing their own engines in JavaScript?&lt;br&gt;
Can we just make our own engine and then, bang, there it is? We’ve got our JavaScript engine. Yes, you can certainly do that.&lt;/p&gt;

&lt;p&gt;But keep in mind that our list over here was labelled ECMAScript engines, not JavaScript engines. Because if everyone could just design a JavaScript engine, it would be complete pandemonium, which is why ECMAScript was formed. It tells folks, “Hey, here’s the standard for how to do things in JavaScript and how it should function.”&lt;/p&gt;

&lt;p&gt;And the script is JavaScript’s governing body, which effectively chooses how the language should be standardised. So it tells engine creators that this is how JavaScript should function, but how you create the engine internally is up to you as long as it adheres to the standards.&lt;/p&gt;

&lt;p&gt;And, to return to our example, as an engine designer, I can do whatever I want with this engine inside of this box as long as it adheres to my script rules. As a result, businesses and organisations compete to have the fastest engine so that people will use their product. For example, Google is keen on having the fastest engine possible so that more people use their Chrome browsers and their search engine, allowing them to sell more ads and generate more cash.&lt;/p&gt;

&lt;p&gt;However, keep in mind that this may be implemented in any way we choose and that it is always changing in order to find the quickest way to have JavaScript work in our browsers or even outside of our browsers. And we’ll be talking about the V8 engine because it’s the most common. And you might see various variants on this. However, what we’ll cover here applies to the vast majority of JavaScript engines. And, while things may change over time, as long as you grasp the fundamentals and why it’s structured the way it is, we’ll be able to produce optimal code.&lt;/p&gt;

&lt;p&gt;But, before we get there, let's finish the article here, and let’s define what interpreter and compiler are in the next article.&lt;/p&gt;

&lt;p&gt;I will update this article by adding the next part link here, so stay with me :)&lt;/p&gt;

</description>
    </item>
    <item>
      <title>A headache named default style in CSS</title>
      <dc:creator>Alireza Hamid</dc:creator>
      <pubDate>Sun, 30 May 2021 12:15:48 +0000</pubDate>
      <link>https://forem.com/alirezahamid/a-headache-named-default-style-element-in-css-179j</link>
      <guid>https://forem.com/alirezahamid/a-headache-named-default-style-element-in-css-179j</guid>
      <description>&lt;p&gt;At least once, Front-end developers tackled default style in their projects and professional life. And also, swearing at CSS creator or Google Chrome and so on as well. So how we can get rid of this nightmare? Is any easy peasy lemon squeezy way to solve the problem?&lt;br&gt;
Yes, this Is the short answer to this question. But how? In this short article or whatever you named, I will introduce how you can reset all the user agent and default styles.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where the default style comes from?
&lt;/h2&gt;

&lt;p&gt;Before understanding the solution, it's important to understand where we get our basic styles in our browser.&lt;br&gt;
All the properties in CSS come with &lt;code&gt;initial&lt;/code&gt; value to load the default style of each property. And this is NOT related to HTML.&lt;/p&gt;

&lt;h2&gt;
  
  
  The User-Agent Browser Styles
&lt;/h2&gt;

&lt;p&gt;After applying the initial styles of all the CSS properties, the browser finally loads its default styles which are known as &lt;code&gt;user-agent&lt;/code&gt;. These styles have nothing to do with the base initial values of the CSS properties.&lt;/p&gt;

&lt;p&gt;Also, you can see them in the Chrome developer tools.&lt;/p&gt;

&lt;p&gt;For Example:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwlnhpzj706yhv8vs395q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwlnhpzj706yhv8vs395q.png" alt="1_user-agent"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;HTML elements do not have initial style values! The basic styles of an HTML element, such as the &lt;code&gt;&amp;lt;h1&amp;gt;&lt;/code&gt; tag, for example, comes from the browser user agent stylesheet and not from the &lt;code&gt;initial&lt;/code&gt; value of the properties of CSS.&lt;/p&gt;

&lt;h2&gt;
  
  
  And now lets to talk about the solution
&lt;/h2&gt;

&lt;p&gt;To reset all the &lt;code&gt;user-agent&lt;/code&gt; styles, you can easily use &lt;code&gt;all:unset&lt;/code&gt; property for each element in the CSS.&lt;br&gt;
For example: &lt;code&gt;h1{all:unset}&lt;/code&gt;&lt;br&gt;
There are different values for &lt;code&gt;all&lt;/code&gt; like &lt;code&gt;initial&lt;/code&gt; and &lt;code&gt;inherit&lt;/code&gt; which you can read about them more in the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/CSS/all" rel="noopener noreferrer"&gt;developer.mozilla.org&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Browser Support
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;inherit&lt;/code&gt; - works in all browsers, including Internet Explorer 11&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;initial&lt;/code&gt; &amp;amp; &lt;code&gt;unset&lt;/code&gt; &amp;amp; &lt;code&gt;revert&lt;/code&gt;- work in all browsers except for Internet Explorer 11&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Final Words
&lt;/h2&gt;

&lt;p&gt;That's all.&lt;br&gt;
I hope you've enjoyed this article and learned from my experience.&lt;br&gt;
If you like this post, I would appreciate applause and sharing :-)&lt;/p&gt;

</description>
      <category>css</category>
      <category>webdev</category>
      <category>html</category>
      <category>web</category>
    </item>
    <item>
      <title>How to fix the CORS issue in Vuejs</title>
      <dc:creator>Alireza Hamid</dc:creator>
      <pubDate>Sat, 19 Dec 2020 22:24:51 +0000</pubDate>
      <link>https://forem.com/alirezahamid/how-to-fix-cors-issue-in-vuejs-545o</link>
      <guid>https://forem.com/alirezahamid/how-to-fix-cors-issue-in-vuejs-545o</guid>
      <description>&lt;p&gt;Imagine the first day in which you want to connect and implement API(s) from the back-end into your front-end project. Suddenly when you call those API(s) something going wrong, and you likely see this error in your console.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Access to XMLHttpRequest at ‘http://api.back.end/data' from origin ‘http://localhost:8080/' has been blocked by CORS policy.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Boom, everything breaks down. This is a nightmare for developers. But how we can deal with this? Before to solve the problem we should deep dive into this error.&lt;/p&gt;

&lt;h1&gt;
  
  
  Why do we get CORS error? What really is CORS?
&lt;/h1&gt;

&lt;p&gt;This is a common policy on the web we call it &lt;em&gt;same-origin&lt;/em&gt;, which means web applications should interact with other services from the same origin. But sometimes it is required to send a request to another service-- server, back-end, API, and etc. CORS makes it possible to set a specific header on the request to the server. you can read widely about &lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS"&gt;Cross-Origin Resource Sharing (CORS)&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In my case, I was serving my Vuejs project on &lt;code&gt;localhost&lt;/code&gt; and the API service was running on the server. As you can guess they're were from different &lt;em&gt;origins&lt;/em&gt; completely against the &lt;em&gt;same-origin&lt;/em&gt; policy as I mentioned. We assume you haven't any access to change the Back-end configuration.&lt;/p&gt;

&lt;h1&gt;
  
  
  so, How we can deal with CORS error?
&lt;/h1&gt;

&lt;p&gt;Yes, we should have &lt;em&gt;same-origin&lt;/em&gt;, But how? Hopefully we can achieve this easily with Vue CLI. In my case, the Front-end project was running on &lt;code&gt;localhost:8080&lt;/code&gt;. We actually need to proxy the API requests to the Back-end during development.&lt;/p&gt;

&lt;p&gt;To config this setting, you should put the proxy URL into this file &lt;code&gt;vue.config.js&lt;/code&gt; if you haven't this file yet in your project, first, you need to create the file right beside the &lt;em&gt;package.json&lt;/em&gt; in the root of the project.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// vue.config.js
module.exports = {
  // options...
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Inside &lt;code&gt;module.exports&lt;/code&gt; you can put your configuration using &lt;code&gt;devServer&lt;/code&gt; object. Because we want to config our development server to proxy API requests to the Back-end Endpoint.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// vue.config.js
module.exports = {
  devServer: {
        proxy: 'http://api.back.end',
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;This will tell the dev server to proxy any unknown requests (requests that did not match a static file) to &lt;a href="http://api.back.end"&gt;http://api.back.end&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now you need to terminate your app and run it again so that new configurations will be applied to the server. Just that, easy peasy lemon squeezy :))&lt;/p&gt;

&lt;p&gt;You can find other configurations here &lt;a href="https://cli.vuejs.org/config/#devserver-proxy"&gt;Vue configurations&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If you want to get rid of this issue temporarily just install this chrome extension &lt;a href="https://chrome.google.com/webstore/detail/cors-unblock/lfhmikememgdcahcdlaciloancbhjino"&gt;CORS Unblock&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>vue</category>
      <category>cors</category>
      <category>frontend</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
