is it safe to access this variable in a function called a finished document
Yes. A variable is declared ( added as a binding to the execution context variable environment) as a very early stage in the execution of this script. (Note that it is not actually assigned a value until the assignment operator is reached during parsing. This is usually called a โraiseโ, but does not affect your example).
Since the execution of the script is executed synchronously (the browser will stop rendering until it has completed the parsing and execution of the script element), the DOM ready event will not fire until this execution is complete.
Edit (question updated)
what then if i do this ...
As described above, a variable declaration will be raised at the top of the scope. Your second example is effectively interpreted as follows:
// Declarations (both function and variable) are hoisted var bar; function callBar() { alert(bar); } $(document).ready(function () { callBar(); }); bar = "foo";
Therefore, these ads are available throughout. The finished event handler will be executed after a while and will have access to these announcements (since they appeared in the same volume as he).
source share