Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What triggers JavaScript code execution?

Tags:

javascript

In case you don't know what I'm talking about, please read John Resig - How JavaScript Timers Work and Is JavaScript guaranteed to be single-threaded?

There are several triggers that enqueue tasks in the JS engines' execution FiFo. This is not part of any standard, so I'm trying to find an exhausive list of those triggers. (I guess it all boils down to internal event handlers, like script load events or timer events, but I'd rather ignore the engine's internals and look at things from the user's point of view.)

So far I have identified

  • <script> elements in the initial document (including the ones added by document.write)*
  • <script> elements inserted by JS at runtime*
  • Event handlers
    -- these include a wide variety of cases, such as user interaction, Error events, Web Worker messages or Ajax callbacks ...
  • window.setTimeout
  • window.setInterval

*) only in Browser/DOM environments

Any more? Any differences between JS engines?

like image 723
user123444555621 Avatar asked Mar 30 '12 08:03

user123444555621


1 Answers

"JavaScript" as a language name should not really be used it is too broad.

ECMAScript is what you are referring to. You can find information on ECMAScript at http://www.ecmascript.org/ The language standard is called ECMA-262 with the 5.1 Edition supported by most browsers.

setTimeout, setInterval, DOM events, and more are not part of the language. These are provided by the host environment as host objects. Writing ECMAScript for a wide range of host environment should take special care when using host objects.

ECMAScript code is executed within an execution context. This takes the form of a stack and will hold the state of the current execution context at the top.

There are 3 ways to push an execution context. Global code, eval, and function. This is the only way to start code. Host environments will use these methods to execute code.

The host environment can supply a call stack. This is used to stack function calls generated by host objects which may run in independent threads. Typically an event such as setTimeout will add a function to the call stack. The host environment will then wait until the execution context stack is empty then pop the function from the call stack, create a new execution context, execute the code until completed. It will repeat this until the call stack is empty.

Attempting to build a comprehensive list of host object execution context managers is futile.

To answer the questions.

Any more? Yes there are many more. This is beyond the scope of this answer. Refer to the particular host environment you wish to use.

Any differences between JS engines? (ECMAScript Host environments). Yes. Again this beyond the scope of this answer and dependent on the host

There are dozens of host environments, with new ones created all the time. What triggers the creation of a new execution context is highly dependent on the host environment.

like image 145
Blindman67 Avatar answered Oct 14 '22 13:10

Blindman67