最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How do browsers parse and interpret JavaScript code? - Stack Overflow

programmeradmin3浏览0评论

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?

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?

Share Improve this question edited May 18, 2012 at 16:01 Sampson 268k76 gold badges545 silver badges568 bronze badges asked Sep 21, 2011 at 14:27 dev.e.loperdev.e.loper 36k77 gold badges167 silver badges257 bronze badges 8
  • 1 Different browsers use different java script engines, such as V8, or spider monkey. – Incognito Commented Sep 21, 2011 at 14:28
  • I imagine they follow some standard. There are some similarities. – dev.e.loper Commented Sep 21, 2011 at 14:29
  • JavaScript code is interpreted, not parsed. – Šime Vidas Commented Sep 21, 2011 at 14:29
  • 9 @ŠimeVidas: Well you certainly have to parse it before you interpret it – Claudiu Commented Sep 21, 2011 at 14:30
  • 3 @dev.e.loper: That standard is ECMA-262: ecma-international.org/publications/standards/Ecma-262.htm. – pimvdb Commented Sep 21, 2011 at 14:30
 |  Show 3 more comments

2 Answers 2

Reset to default 17

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

发布评论

评论列表(0)

  1. 暂无评论