如何在匿名函数构造中获取变量名var f = function(){};

问题描述 投票:0回答:2

所以我有一个像下面的结构(这是虚拟代码)。在分配给someFunction变量的函数内部,我想获得赋予此函数的变量的实际名称。

返回的预期结果将是字符串“someFunction”。

感谢您的专业知识!

我试过这个;并且还没有其他线索。

var someFunction = (function(){
 "use strict"; 
var f = this.FunctionName; //dummy code
return f;
}

期望将“someFunction”字符串作为返回值

var onLoad_123123= (function(GlideAjax,g_form,GlideDialogWindow,GlideRecord,window,document,$,jQuery,$$,$j,$F,gel,undefined){
 "use strict"; 
function onLoad_123123() {
    retun 'onLoad_123123'; //dummy code here - have to get it dynamically
}

希望能更好地解释它

javascript servicenow
2个回答
1
投票

获取和动态返回函数名称的唯一方法是从函数中访问arguments.callee.name

这在严格模式下不可用。如果将函数声明为strict,则在尝试访问该属性时,解释器将抛出错误。


0
投票

据我所知,serviceNow中未启用“use strict”。试试这个:

console.log("internal id: " + arguments.callee.name);
console.log("internal sysId: " + g_event_handler_ids[arguments.callee.name]);

g_event_handler_ids包含目录项的所有脚本。

© www.soinside.com 2019 - 2024. All rights reserved.