How do browsers parse and interpret JavaScript code?
How does a browser go about parsin开发者_StackOverflow社区g 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?
This is defined in the ECMAScript standard.
First the source text (the stuff between the <script>
tags) is converted into a series of tokens (according to the Lexical Grammar of the language):
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.
Read here: http://es5.github.com/#x7
That series of tokens is treated as a Program, which is then evaluated according to the Syntactic Grammar of the language which is defined in chapters 11 to 14 of the ECMAScript standard.
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.
Read here: http://es5.github.com/#x5.1.4
It starts in chapter 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?
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
精彩评论