I have a task that depends upon function.caller to sanity check that a caller is authorized.
According to this url, caller is supported in all major browsers... and all of my unit tests pass:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/caller
However, nodejs rejects all attempts to access function.caller (reports it as null).
I'm open to suggestions to make this work on nodejs... I'd hate to have this framework only work on browsers.
Thanks!
A function can be executed by calling the execute() method in which the function ID and configuration (of type JSON) are passed as parameters. The functions reference used in the code snippets below is the component instance.
Using the function.caller property in JavaScript. It returns the name of the function from which the current function is invoked. It returns the null value if the function is invoked from the top-level JavaScript code.
The function. caller property of the JavaScript function object returns the function that invoked the specified function. It returns null if the function “f” was invoked by the top-level code in JavaScript. For functions like strict functions, async functions and generator function this method returns null.
setTimeout() can be used to schedule code execution after a designated amount of milliseconds. This function is similar to window. setTimeout() from the browser JavaScript API, however a string of code cannot be passed to be executed.
The Caller-id npm package makes it as easy as pie : https://www.npmjs.com/package/caller-id from the docs :
var callerId = require('caller-id');
// 1. Function calling another function
function foo() {
bar();
}
function bar() {
var caller = callerId.getData();
/*
caller = {
typeName: 'Object',
functionName: 'foo',
filePath: '/path/of/this/file.js',
lineNumber: 5,
topLevelFlag: true,
nativeFlag: false,
evalFlag: false
}
*/
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With