Get a function's name as a JSString.

Syntax

JSString *
JS_GetFunctionId(JSFunction *fun);

JSString *
JS_GetFunctionDisplayId(JSFunction *fun); // Added in Spidermonkey 17
Name Type Description
fun JSFunction * A JavaScript function.

Description

JS_GetFunctionId returns the name of a function, fun, as a JSString, or NULL if fun is unnamed. The returned string lives as long as fun, so you don't need to root a saved reference to it if fun is well-connected or rooted, and provided you bound the use of the saved reference by fun's lifetime.

JS_GetFunctionDisplayId returns the display name of a function, fun. This is the defined name if one was given where the function was defined, or it could be an inferred name by the JS engine in the case that the function was defined to be anonymous. This can still return nullptr if a useful display name could not be inferred. The same restrictions on rooting as those in JS_GetFunctionId apply.

Examples

The name returned by JS_GetFunctionDisplayId is the same as returned by JS_GetFunctionId if the function explicitly has a name listed in the source, or if not it is an intelligently guessed name for the function based on its context in the source. If no smart name could be guessed for the function, then NULL is returned. The returned string is guaranteed to live as long as fun, so the application often does not need to root the string.

function f() {}                     // f
var x = function() {};              // x

var a = { b: function() {} };       // a.b
a.c = function() {};                // a.c

a.d = (function() {                 // a.d<  -- the '<' loosely means "contributor to"
  return function() {};             // a.d
})();

var Foo = function() {              // Foo
  var bar = function() {};          // Foo/bar
};

setTimeout(function() {}, 100);     // no name, JS_GetFunctionDisplayId returns NULL

See Also