For the example code below
/**
* @param {object} settingsTable A table to configure settings.
*/
function example(settingsTable) {
if ( !(settingsTable && typeof(settingsTable) == "object")) {
settingsTable = {};
}
// Default values to force
if (!settingsTable.bEnableSomething) {
settingsTable.bEnableSomething = false
}
if (!settingsTable.text) {
settingsTable.text = "Default text"
}
// Function
if (settingsTable.bEnableSomething) {
console.log(settingsTable.text)
} else {
console.log("Function disabled.")
}
}
To explain what this example code does.
You have a function called example that wants a value by the type of an object.
This table gets used as a configuration table, to customize the output of the function.
There are following settings you can change.
settingsTable.bEnableSomething - will make it so that "text" from settingsTable gets print out if set to true.
settingsTable.text - to change the "text" value from the settingsTable object.
What would have to be done or how should the code be changed, so that it would be better for this type of stuff, with the concept of having a "settingsTable" (not exactly sure if there's a set term for this)
What I was wondering on how to achieve, is that:
Basically, when you use:
example({bEnableSomething: true, text: "Hello"})
It will run the function and log out "Hello"
and I provided that what I defined for the "settingsTable"
which is settingsTable.bEnableSomething and settingsTable.text
the problem is, the program didn't suggest it, as example Google Chrome or Visual Studio Code
So basically what I am wondering about is, how can I change the code so that somebody else that would use a module with this concept, that the application shows them possible inputs for the settingsTable
such as settingsTable.bEnableSomething and more, not just the ones in the example?
What can be done to achieve that? Does it have do be done with classes? Does JSDoc have a feature for it?
What is this even called?
To understand what the settingsTable is, it's a configuration table. While you could use function parameters. Should you end up having some sort of object creations with a lot of parameters, that you don't know if you're going to change the order of, then I think a table to change those parameters, would be better. Because you do not have to worry about the order, since you just need to use the "key" to change a value.
If you develop a module, and change the order of the parameters of a function, you may end up breaking other peoples code.
Would the way below, be a proper way?
class settingsTableTemplate {
/**
* When set to "true", will console.log out "text".
* @default false
*/
bEnableSomething = false
/**
* Text to log out when ``bEnableSomething`` is set to "true".
* @default "Default text"
*/
text = "Default text"
}
/**
* @param {settingsTableTemplate} settingsTable A table to configure settings.
*/
function example(settingsTable) {
if ( !(settingsTable && typeof(settingsTable) == "object")) {
settingsTable = {};
}
// Default values to force
if (!settingsTable.bEnableSomething) {
settingsTable.bEnableSomething = false
}
if (!settingsTable.text) {
settingsTable.text = "Default text"
}
// Function
if (settingsTable.bEnableSomething) {
console.log(settingsTable.text)
} else {
console.log("Function disabled.")
}
}
It would now allow you to press CTRL + Space in Visual Studio Code as example, and it would suggest the options settingsTable.bEnableSomething and settingsTable.text when pressing space, within the brackets at example({})
The class doesn't get used within the function though.