JS_GetFunctionId 编辑

Get a function's name as a JSString.

Syntax

JSString *
JS_GetFunctionId(JSFunction *fun);

JSString *
JS_GetFunctionDisplayId(JSFunction *fun); // Added in Spidermonkey 17
NameTypeDescription
funJSFunction *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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:133 次

字数:3858

最后编辑:7年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文