JavaScript function name inference aka stop function naming mayhem

When you are a Firefox developer you are probably familiar with the interesting way of obtaining a stack-trace from JavaScript code.

(new Error()).stack
/* or */

And historically unnamed (anonymous) functions made the stack-trace totally useless, so for example Firefox code has the policy to name such functions instead. Here is a small example.

var Sandbox = {
execute: function () {
throw "Unsafe!";
try {
} catch (error) {

Historically this produced a stack-trace like this (executed in Scratchpad):


But in Firefox 17 you should see something like this. (This was implemented in Bug 433529)


So you don’t need to make up names like “SandboxExcute” for the function and still get something readable. At the moment Components.stack doesn’t yet include this magic, but it should land soon (Bug 805222). This name inference mechanism should be able to handle a few more cases, for details look at the first bug, I linked.

Tags: mozilla