D
Deno

help

Can Deno list all functions within a script without explicit registration or annotation?

BBairdy1/14/2024
. For context, here is how you do it for globalThis:
Object.getOwnPropertyNames(globalThis)
.filter((item: string) => typeof (globalThis as any)[item] === "function")
.forEach((funcName: string) => console.log(funcName));
Object.getOwnPropertyNames(globalThis)
.filter((item: string) => typeof (globalThis as any)[item] === "function")
.forEach((funcName: string) => console.log(funcName));
Output:
Object
Function
Array
Number
parseFloat
parseInt
Boolean
String
Symbol
Date
Promise
RegExp
Error
AggregateError
EvalError
RangeError
ReferenceError
SyntaxError
TypeError
URIError
ArrayBuffer
Uint8Array
...
Object
Function
Array
Number
parseFloat
parseInt
Boolean
String
Symbol
Date
Promise
RegExp
Error
AggregateError
EvalError
RangeError
ReferenceError
SyntaxError
TypeError
URIError
ArrayBuffer
Uint8Array
...
But in Javascript it does show my functions:
Object.getOwnPropertyNames(window)
.filter(item => typeof window[item] === "function")
.forEach(funcName => console.log(funcName));
Object.getOwnPropertyNames(window)
.filter(item => typeof window[item] === "function")
.forEach(funcName => console.log(funcName));
Output:
...
testFunction1
testFunction2
...
testFunction1
testFunction2
DAHDeno AI Helper1/14/2024
I wasn't able to find the answer to your question in the official Deno documentation. However, based on the code snippet you provided, it seems like you're trying to list functions in a similar manner to how you would in a browser environment with window. In Deno, there isn't a direct equivalent to the window object that you would find in a browser, as Deno does not have a windowing system—it's a runtime for executing JavaScript outside of the browser. However, the globalThis object is the standard way to access the global scope in both the browser and Deno. If you're trying to list all functions within a specific script, you would need to have a reference to the module or context where these functions are defined. Unlike in a browser environment, where all global functions are attached to the window object, in Deno, module-scoped functions are not automatically attached to globalThis. If you're looking to list functions that are explicitly exported from a module, you could import the module and then use reflection (like Object.keys or similar methods) to list the functions. Here's an example of how you might attempt to list exported functions from a module:
import * as myModule from './myModule.ts';

Object.keys(myModule)
.filter((key: string) => typeof myModule[key] === 'function')
.forEach((funcName: string) => console.log(funcName));
import * as myModule from './myModule.ts';

Object.keys(myModule)
.filter((key: string) => typeof myModule[key] === 'function')
.forEach((funcName: string) => console.log(funcName));
Please note that this advice is not based on the Deno documentation, and I would advise you to double-check the accuracy of this advice. If you're trying to list functions that are not exported or are within a local scope of a module, you would not be able to do this without explicit registration or annotation, as JavaScript does not provide a way to introspect local scopes at runtime.
Jjcayzac1/14/2024
Try with this instead of globalThis, maybe?
BBairdy1/14/2024
Apparently deno doc is a command that does something very similar. Searching for the wrong keywords I guess

Looking for more? Join the community!