admin管理员组

文章数量:1290234

I'm an admitted novice JavaScript programmer and am attempting to learn more. So I turn to you folks for help, with this easy question :). The O'Reilly book that I'm reading keeps referring to the pile-time of the JavaScript code. My knowledge of functional programming (scheme and the likes) tells me that the JavaScript is actually interpreted by the browser, most likely requiring two passes through the JavaScript.

Am I incorrect in my assessment? Or is the pile-time that the book references actually just the first pass of the interpreter, similar to how Perl or Python would function? Thanks!

I'm an admitted novice JavaScript programmer and am attempting to learn more. So I turn to you folks for help, with this easy question :). The O'Reilly book that I'm reading keeps referring to the pile-time of the JavaScript code. My knowledge of functional programming (scheme and the likes) tells me that the JavaScript is actually interpreted by the browser, most likely requiring two passes through the JavaScript.

Am I incorrect in my assessment? Or is the pile-time that the book references actually just the first pass of the interpreter, similar to how Perl or Python would function? Thanks!

Share Improve this question asked Aug 31, 2011 at 2:56 wfosterwfoster 79110 silver badges23 bronze badges 1
  • Most dynamic language implementations will first "parse" the input into an Abstract Syntax Tree of sorts -- this is the part where syntax errors are detected. After that the AST can be run ("walked") directly or it can be converted into some kind of internal bytecode and/or JIT'ed. The "how" to run is not specified in the ECMAScript Specification and varies greatly between different implementations. – user166390 Commented Aug 31, 2011 at 3:20
Add a ment  | 

3 Answers 3

Reset to default 11

It is browser-dependent. Look up WebKit's SquirrelFish Extreme and Google V8 to see what's at the fastest end of things, and look at Mozilla's JaegerMonkey for that implementation.

AFIAK V8 and SFX are JITs, so they pile JS code to native. JaegerMonkey and TraceMonkey bine in Firefox to form a system where if code would be faster traced, TraceMonkey executes it, and if code were faster native, JaegerMonkey piles it, just like SFX.

Do you have a sentence that you could quote to help with context?

Javascript is piled at the browser (it's sent to the browser in plain source). But it only gets piled as it is loaded. So if you have a script tag followed by a div tag followed by a script tag then it will load those things sequentially. The browser will stop loading the entire page (it still downloads resources, just doesn't load HTML) until your script has been loaded (this is because the script may have 'document.write' within it).

<script>
var someVariable = 'hello world';
alert(document.getElementById('someid')); //alerts undefined
</script>

<div id='someid'></div>

<script>
alert(document.getElementById('someid')); //alerts 'someid'
alert(someVariable); //alerts 'hello world'
</script>

There's read-time and run-time in JS (as I like to think of it, since it's not really piled, but interpreted). It sounds like the O'Reilly book is using pile-time as a synonym for read-time.

Read-time is when the engine reads all of the code and evaluates everything at the global scope. Usually this sets up hooks on events that will trigger code execution.

Run-time is everything else.

本文标签: functional programmingDoes Javascript compile or twopass interpretStack Overflow