Compiler Theory in JavaScript

Source:https://github.com/getify/You-Dont-Know-JS/blob/master/scope%20&%20closures/ch1.md

Common Compliation Steps

  • Tokenizing/Lexing: breaking up a string of characters into meaningful (to the language) chunks, called tokens. For instance, consider the program: var a = 2;. This program would likely be broken up into the following tokens: vara=2, and ;. Whitespace may or may not be persisted as a token, depending on whether it‘s meaningful or not.
    Note: The difference between tokenizing and lexing is subtle and academic, but it centers on whether or not these tokens are identified in a stateless or stateful way. Put simply, if the tokenizer were to invoke stateful parsing rules to figure out whether a should be considered a distinct token or just part of another token, that would be lexing.
  • Parsing: taking a stream (array) of tokens and turning it into a tree of nested elements, which collectively represent the grammatical structure of the program. This tree is called an "AST" (Abstract Syntax Tree). The tree for var a = 2; might start with a top-level node called VariableDeclaration, with a child node called Identifier (whose value is a), and another child called AssignmentExpression which itself has a child called NumericLiteral (whose value is 2).
  • Code-Generation: the process of taking an AST and turning it into executable code. This part varies greatly depending on the language, the platform it‘s targeting, etc.

Complier Model in JS

  • Engine: responsible for start-to-finish compilation and execution of our JavaScript program.Compiler: one of Engine‘s friends; handles all the dirty work of parsing and code-generation (see previous section).
  • Scope: another friend of Engine; collects and maintains a look-up list of all the declared identifiers (variables), and enforces a strict set of rules as to how these are accessible to currently executing code.

LHS & RHS

  • An RHS look-up is indistinguishable, for our purposes, from simply a look-up of the value of some variable.(get the value of)
  • An LHS look-up is trying to find the variable container itself, so that it can assign.
  • In this way, RHS doesn‘t really mean "right-hand side of an assignment" per se, it just, more accurately, means "not left-hand side".
function foo(a) {
    console.log( a ); // RHS of console Object, LHS of parameter in log
}
foo( 2 ); // RHS of foo
// inplied a = 2 // LHS of a
function foo(a) {
    var b = a; // LHS of b, RHS of a
    return a + b; // RHS of a, RHS of b
}
var c = foo( 2 ); // LHS of c, RHS of foo
// implied a = 2 // LHS of a

Nested Scope

function foo(a) {
    console.log( a + b );
}
var b = 2;
foo( 2 ); // 4

The RHS reference for b cannot be resolved inside the function foo, but it can be resolved in the Scope surrounding it (in this case, the global).

Error

function foo(a) {
    console.log( a + b ); // RHS can not find b in the scopes, results in a ReferenceError being thrown by the Engine
    b = a;
}
foo( 2 );

If the Engine is performing an LHS look-up, and it arrives at the top floor (global Scope) without finding it, if the program is not running in "Strict Mode" [^note-strictmode], then the global Scope will create a new variable of that name in the global scope, and hand it back to Engine.

时间: 2024-11-10 13:01:18

Compiler Theory in JavaScript的相关文章

使用Google Closure Compiler高级压缩Javascript代码注意的几个地方

介绍 GCC(Google Closure Compiler)是由谷歌发布的Js代码压缩编译工具.它可以做到分析Js的代码,移除不需要的代码(dead code),并且去重写它,最后再进行压缩. 三种压缩模式 GCC提供三种压缩模式: 1)Whitespace only 2)Simple 3)Advanced 我们以这段简单的代码为例 function sayHello(name) { alert('Hello, ' + name); } sayHello('binnng'); 分别使用这三种压

Web优化之Javascript Compressor

目的: web项目前端js的体积大小影响页面性能和用户体验, 压缩js是web优化的一个重要手段.JavaScript的压缩不是为了保护代码而压缩,而是压缩后的js代码文件可以小一倍甚至多倍,从而使这个js代码快速的下载到客户端,特别js文件较大时速度效果非常明显. 方法: JS优化的方法有以下几个方法: 1.tomcat的压缩, gzip压缩启用的情况下, 压缩的比例大概是1:6 2.js加载的时候合并下载, 可以有效减少请求js产生的网络连接次数.(满足Yahoo!前端优化第一条原则:Min

程序员必读书单

作者:Lucida 微博:@peng_gong 豆瓣:@figure9 原文地址:http://www.cnblogs.com/figure9/p/developer-reading-list.html 关于 本文把程序员所需掌握的关键知识总结为三大类19个关键概念,然后给出了掌握每个关键概念所需的入门书籍,必读书籍,以及延伸阅读.旨在成为最好最全面的程序员必读书单. 前言 Reading makes a full man; conference a ready man; and writing

程序员必读书

前言 Reading makes a full man; conference a ready man; and writing an exact man. Francis Bacon 优秀的程序员应该具备两方面能力: 良好的程序设计能力: 掌握常用的数据结构和算法(例如链表,栈,堆,队列,排序和散列): 理解计算机科学的核心概念(例如计算机系统结构.操作系统.编译原理和计算机网络): 熟悉至少两门以上编程语言(例如C++,Java,C#,和Python): 专业的软件开发素养: 具备良好的编程

程序员必读书单(转)

作者:Lucida 微博:@peng_gong 豆瓣:@figure9 原文链接:http://lucida.me/blog/developer-reading-list/ 关于 本文把程序员所需掌握的关键知识总结为三大类19个关键概念,然后给出了掌握每个关键概念所需的入门书籍,必读书籍,以及延伸阅读.旨在成为最好最全面的程序员必读书单. 前言 Reading makes a full man; conference a ready man; and writing an exact man.

程序员必读书目

作者:Lucida 微博:@peng_gong 豆瓣:@figure9 原文链接:http://lucida.me/blog/developer-reading-list/ 关于 本文把程序员所需掌握的关键知识总结为三大类19个关键概念,然后给出了掌握每个关键概念所需的入门书籍,必读书籍,以及延伸阅读.旨在成为最好最全面的程序员必读书单. 前言 Reading makes a full man; conference a ready man; and writing an exact man.

程序员必读书单 1.0

程序员必读书单 1.0 发表于 2015-02-25   |   分类于 阅读  |   暂无评论 转自:http://zh.lucida.me/blog/developer-reading-list/ 本文把程序员所需掌握的关键知识总结为三大类19个关键概念,然后给出了掌握每个关键概念所需的入门书籍,必读书籍,以及延伸阅读.旨在成为最好最全面的程序员必读书单. 前言 Reading makes a full man; conference a ready man; and writing an

程序猿必读书单

作者:Lucida 微博:@peng_gong 豆瓣:@figure9 原文链接:http://lucida.me/blog/developer-reading-list/ 于 本文把程序员所需掌握的关键知识总结为三大类19个关键概念,然后给出了掌握每个关键概念所需的入门书籍,必读书籍,以及延伸阅读.旨在成为最好最全面的程序员必读书单. 前言 Reading makes a full man; conference a ready man; and writing an exact man. F

【转】程序员必读书单 1.0

原文链接:http://lucida.me/blog/developer-reading-list/ 本文把程序员所需掌握的关键知识总结为三大类19个关键概念,然后给出了掌握每个关键概念所需的入门书籍,必读书籍,以及延伸阅读.旨在成为最好最全面的程序员必读书单. 前言 Reading makes a full man; conference a ready man; and writing an exact man. Francis Bacon 优秀的程序员应该具备两方面能力: 良好的程序设计能