How do browsers parse and interpret JavaScript code?

后端 未结 2 1339
臣服心动
臣服心动 2020-12-31 05:12

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 ha

相关标签:
2条回答
  • 2020-12-31 05:31

    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?

    0 讨论(0)
  • 2020-12-31 05:34

    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 讨论(0)
提交回复
热议问题