Understanding the JavaScript Engine — Part 1

 

I have been a Ruby on Rails developer for the last 2 years. I have used JavaScript both in its vanilla form and in some frameworks. However, I learned JavaScript as most new programmers do, by going through a course, without quite understanding how the JavaScript engine works.

Before diving deep into JavaScript, I decided to take some time off and understand its core working principles. I’ll be sharing what I’ve learned so far in this, and subsequent blog posts.

First, let me define some terms you’ll come across. I’ll add examples where necessary.

Syntax Parser

When you write code, a compiler converts your code into a set of instructions that the computer can understand. Part of the compiler is what is known as a syntax parser. The syntax parser goes through your code character by character, and determines if the syntax is valid or not.

Lexical Environment

In simple terms, a Lexical environment refers to where something sits physically in your code. Where something is written gives an idea of how the computer will interpret it and how it will interact with other variables and functions e.g

 
function hello() {
var greet = "Hello world"
}

  

In the function above, we can say that var greet sits lexically in the function.

Execution Context

This is a wrapper that helps manage the code that is running. Looking at the gif below, you can see there’s an execution context stack and in it, we have a Global execution context. When functionA() is called, it is added to the Stack meaning that functionA() is currently being executed. The same goes for functionB().

 

The execution context is created in two phases:

The first phase is called the creation phase. The global execution context creates two things for you, that you don’t have in your code; a global object(window) and a special variable called this. The window object is a global object inside a browser. This object is different depending on whether you are using node or running JavaScript on the server. But there is always a global object when you’re running JavaScript. Take a look at the following image from a browser console:

 

When you create a variable and function that is not inside a function, those variables and functions get attached to the global object.

 
var name = "Debby";

function greet() {
console.log("Hello", name)
}

  

If you run the above JavaScript code in the browser and you inspect the global object, you will see that the variable and the function were added to it.

 

During the creation phase, the syntax parser recognizes where you have defined variables and functions. It therefore sets up memory space for the variables and functions. It’s not actually moving code to the top of the page. What this means is that before your code begins to be executed line by line, the JavaScript engine has already set aside memory space for the variables and functions that you’ve written. This is what is called Hoisting.

The next phase is the execution phase, where assignments are made. When the JavaScript engine sets up memory space for variables, it doesn’t know which values will be stored in them. Therefore, it puts a placeholder called undefined. That placeholder means; I don’t know what this value is yet. All variables in JavaScript are initially set to undefined while functions sit in memory in their entirety. This is why it’s possible to declare a variable without assigning it and the JavaScript engine will not throw any error.

 

This is just a brief introduction to how the JavaScript engine executes the code you write. To know more, you can use the resources below:

JavaScript: Understanding the Weird Parts by Anthony Alicea on Udemy.Execution context, Scope chain and JavaScript internals by Rupesh Mishra.

Understanding the JavaScript Engine—— two phase的更多相关文章

  1. 「2014-3-13」Javascript Engine, Java VM, Python interpreter, PyPy – a glance

    提要: url anchor (ajax) => javascript engine (1~4 articles) => java VM vs. python interpreter =& ...

  2. Javascript Engine, Java VM, Python interpreter, PyPy – a glance

    提要: url anchor (ajax) => javascript engine (1~4 articles) => java VM vs. python interpreter =& ...

  3. Browser Render Engine & Javascript Engine

    Browser Render Engine Programming Language Open Source Javascript Engine Comparation for CSS Compati ...

  4. JavaScript Engine 可视化

    JavaScript Engine 可视化 图解 JavaScript Engine JavaScript 可视化 (7 部曲) ️ JavaScript Visualized: Event Loop

  5. Understanding Delegated JavaScript Events

    While I ended up using a CSS-only implementation for this pen, I started by writing it mostly using ...

  6. v8 javascript engine

    https://code.google.com/p/v8-wiki/wiki/BuildingWithGYP vs2013git v8 http://github.com/v8/v8-git-mirr ...

  7. Attacking JavaScript Engines: A case study of JavaScriptCore and CVE-2016-4622(转)

    转:http://phrack.org/papers/attacking_javascript_engines.html Title : Attacking JavaScript Engines: A ...

  8. Bring JavaScript to your Java enterprise with Vert.x

    转自:https://opensource.com/article/18/4/benefits-javascript-vertx If you are a Java programmer, chanc ...

  9. JavaScript Interview Questions: Event Delegation and This

    David Posin helps you land that next programming position by understanding important JavaScript fund ...

随机推荐

  1. Es6懒加载

    const Login = resolve => require(['@/components/Login'], resolve) 注(当路由被访问的时候才加载这个组件)

  2. 在内网环境使用WPAD/PAC和JS攻击win10

    转:https://mp.weixin.qq.com/s/qoEZE8lBbFZikKzRTwgdsw 在内网环境使用WPAD/PAC和JS攻击win10 2018-03-01 wangrin 看雪学 ...

  3. Java导出数据生成Excel表格

    事先准备: 工具类: package com.wazn.learn.util.export; import java.sql.Connection; import java.sql.DriverMan ...

  4. shell top解析

    top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管理器. top显示系统当前的进程和其他状况,是一个动态显示过程,即可以通过用户按键来不 ...

  5. 导航控制器(UINavigationController)

    导航控制器管理一系列显示层次型信息的场景.它创建一个视图管理器"栈",栈底为根视图控制器,用户在场景间切换时,依次将试图控制器压入栈中,且当前场景的试图控制器位于栈顶.要返回上一级 ...

  6. Vue 2.0学习(二)数据绑定

    Vue实例对象 创建一个vue应用很简单,通过构造函数Vue就能创建一个Vue的根实例: var app = new Vue({ el: '#app', data: { message: 'Hello ...

  7. FastReport.Net使用:[28]数据合并

    基础数据 1.拖动数据源中的数据列到报表设计器中,获得一张简单的报表. 2.下面使用两种方法将期中考试和期末考试的成绩合并到一行显示 合并数据(分组方法) 1.按学生名字和科目来进行分组,成绩文本框咱 ...

  8. 「CTSC2016」单调上升路径

    「CTSC2016」单调上升路径 解题思路:根据提示可以得到答案的下界是 \(n - 1\) ,然后打表发现这个下界好像一定可以取到. 事实上考虑 \(n\) 个点完全图的边数是 \(\frac{n( ...

  9. [POI2005]A Journey to Mars --- 单调队列

    [POI2005]A Journey to Mars 题目描述: Byteazar 决定去火星参加一个空间站旅行. 火星的所有空间站都位于一个圆上. Byteazar 在其中一个登陆然后变开始饶圈旅行 ...

  10. mof提权原理及实现

    关于 mof 提权的原理其实很简单,就是利用了c:/windows/system32/wbem/mof/目录下的 nullevt.mof 文件,每分钟都会在一个特定的时间去执行一次的特性,来写入我们的 ...