0

So I have a construction like below (this is dummy code). Inside of function assigned to someFunction variable I'd like to get actual name of variable that this function is assigned to.

Expected result of return would be string "someFunction".

Thank you for your expertise!

I've tried this; and have no other clue yet.

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

expected to have "someFunction" string as return

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
}

Hope that explains it better

wojasso
  • 17
  • 3
  • Possible duplicate of [Get function name in JavaScript](https://stackoverflow.com/questions/3178892/get-function-name-in-javascript) – Mike Doe Mar 25 '19 at 23:41
  • 2
    I can provide examples that demonstrate why this is a weird question, but before I do that I'd like to know which problem you are trying to solve here. – Felix Kling Mar 25 '19 at 23:42
  • I don't think it's possible - also, your IIFE is invalid (missing `)()`) – Jack Bashford Mar 25 '19 at 23:43
  • @charlietfl: `return someFunction.name;` would work, but of course that just returns `"someFunction"`, so they might as well write `return "someFunction";`. – Felix Kling Mar 25 '19 at 23:43
  • @JackBashford: I don't think it's supposed to be an IIFE, since they say *"Inside of function assigned to someFunction"*. – Felix Kling Mar 25 '19 at 23:44
  • @FelixKling will only work in non-strict mode tho. We could use `arguments.callee.name` if we were not in strict mode, which would exactly fulfi the requirement. – jAndy Mar 25 '19 at 23:44
  • It looks like an IIFE though – Jack Bashford Mar 25 '19 at 23:44
  • @jAndy: Why should that only work in non-strict mode? *edit:* `arguments.callee.name` only works in non-strict mode, yes. But `someFunction.name` works anywhere (but is pointless). – Felix Kling Mar 25 '19 at 23:45
  • @FelixKling `callee` and `caller` properties are prohibited in strict mode and will throw an error on access. I thought you were suggesting the same, because as you already mentioned, `someFunction.name` is pretty pointless. – jAndy Mar 25 '19 at 23:46
  • @jAndy: Hence my first comment ("examples that demonstrate why this is a weird question") :) – Felix Kling Mar 25 '19 at 23:48
  • A mandatory reminder: it's OP's responsibility to share their problem not the community's to guess. I know you're good at guessing, but still ;-P – zerkms Mar 25 '19 at 23:49
  • 2
    That still doesn't explain why you need the function name. It's not quite clear which code is your own and which comes from the platform/service you are using. – Felix Kling Mar 25 '19 at 23:50
  • Edited question. Maybe that will help you understand what is needed. Appreciate your help – wojasso Mar 25 '19 at 23:51
  • 1
    No, it does not explain why you need the function name. – Felix Kling Mar 25 '19 at 23:55
  • @FelixKling Are you familiar with ServiceNOW? If not, your help and comments won't be needed anymore. – wojasso Mar 26 '19 at 00:06
  • 1
    Good luck finding people who want to help with that attitude. Answering a simple question would be so much more productive than attacking others, especially considering that you want something from us. – Felix Kling Mar 26 '19 at 00:48

2 Answers2

1

The only way to get and return a function name "dynamically" is to access arguments.callee.name from within the function.

This won't be available in strict mode. If you declare a function as strict, your interpreter will throw on error when you try to access that property.

jAndy
  • 231,737
  • 57
  • 305
  • 359
  • It is inside of ServiceNOW - don't have influence on that use strict; line - need other solution – wojasso Mar 25 '19 at 23:52
  • 3
    "need other solution" --- or better, other design. – zerkms Mar 25 '19 at 23:53
  • 2
    I'm afraid there is no other solution. The access to those properties `callee` and `caller` were removed on introducing strict mode (for good security reasons). As @zerkms mentioned, you should reconsider whatever you want to achieve. – jAndy Mar 25 '19 at 23:56
  • note also that `const foo = function() { console.log(arguments.callee.name); }; foo(); // logs foo` but `const foo = () => { console.log(arguments.callee.name); }; foo(); // Error: arguments is not defined` – Nino Filiu Mar 25 '19 at 23:58
  • @zerkms very helpful - tell it to servicenow.com – wojasso Mar 26 '19 at 00:05
  • @wojasso what is really "helpful" - is not sharing a problem details for half an hour then wondering why you did not get the answer you want. – zerkms Mar 26 '19 at 00:07
  • @zerkms I don't think you can be more helpful than you've been so far - please use your time to answer on other questions. Best Regards – wojasso Mar 26 '19 at 00:11
  • In conclusion. Using `use strict;` and trying to retrieve function name is not possible at all? So now to ServiceNOW experts. Is it possible in any other way to retrieve Client Script sys_id / Client Script name or any other identifier within its script body? – wojasso Mar 26 '19 at 00:09
0

As far as I know "use strict" isn't enabled in serviceNow. So try this:

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

g_event_handler_ids holds all scripts for the catalog item.

Gordon Mohrin
  • 645
  • 1
  • 6
  • 17