浏览器如何解析和解释 JavaScript 代码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7501520/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do browsers parse and interpret JavaScript code?
提问by dev.e.loper
How does a browser go about parsing JavaScript it loads from files or inline? I am trying to get at the core of what a browser does. What happens when a page loads and it has <script>
references to external files, and actual JavaScript on the page too. Any good articles out there?
浏览器如何解析从文件或内联加载的 JavaScript?我试图了解浏览器的核心功能。当页面加载并<script>
引用外部文件以及页面上的实际 JavaScript时会发生什么。有什么好文章吗?
采纳答案by ?ime Vidas
This is defined in the ECMAScript standard.
这在 ECMAScript 标准中定义。
First the source text (the stuff between the <script>
tags) is converted into a series of tokens (according to the Lexical Grammarof the language):
首先将源文本(<script>
标签之间的内容)转换为一系列标记(根据语言的词法语法):
The source text of an ECMAScript program is first converted into a sequence of input elements, which are tokens, line terminators, comments, or white space. The source text is scanned from left to right, repeatedly taking the longest possible sequence of characters as the next input element.
ECMAScript 程序的源文本首先被转换为一系列输入元素,它们是标记、行终止符、注释或空格。源文本从左到右扫描,重复地将最长可能的字符序列作为下一个输入元素。
Read here: http://es5.github.com/#x7
在这里阅读:http: //es5.github.com/#x7
That series of tokens is treated as a Program, which is then evaluated according to the Syntactic Grammarof the language which is defined in chapters 11 to 14 of the ECMAScript standard.
该系列标记被视为一个程序,然后根据ECMAScript 标准第 11 至 14 章中定义的语言语法语法对其进行评估。
The syntactic grammar for ECMAScript is given in clauses 11, 12, 13 and 14. This grammar has ECMAScript tokens defined by the lexical grammar as its terminal symbols (5.1.2). It defines a set of productions, starting from the goal symbol Program, that describe how sequences of tokens can form syntactically correct ECMAScript programs.
ECMAScript 的句法文法在第 11、12、13 和 14 条中给出。该文法具有由词法文法定义的 ECMAScript 标记作为其终结符 (5.1.2)。它定义了一组产生式,从目标符号 Program 开始,描述了标记序列如何形成语法正确的 ECMAScript 程序。
Read here: http://es5.github.com/#x5.1.4
在这里阅读:http: //es5.github.com/#x5.1.4
It starts in chapter 14: http://es5.github.com/#x14
它从第 14 章开始:http: //es5.github.com/#x14
Note that each <script>
element represents a separate JavaScript program.
Read here: How many JavaScript programs are executed for a single web-page in the browser?
请注意,每个<script>
元素代表一个单独的 JavaScript 程序。
在这里阅读:在浏览器中为单个网页执行了多少 JavaScript 程序?
回答by Dave Chapman
This is probably the best description of what a browser does according to the ECMAScript standard Javascript Closures: Identifier Resolution, Execution Contexts and scope chains
根据 ECMAScript 标准Javascript 闭包:标识符解析、执行上下文和作用域链,这可能是对浏览器功能的最佳描述