I have a function that returns boolean if tree has at least one enebled value:
JavaScript
x
15
15
1
treeHasEnabledNode(): Function {
2
let enabled = false;
3
4
return function isNodeEnabled(node: T): boolean {
5
if (!node || !node.children || !node.children.length) return enabled;
6
if (node.enabled && node.enabled !== undefined) return true;
7
8
node.children.forEach((node: T) => {
9
enabled = isNodeEnabled(node);
10
});
11
12
return enabled;
13
};
14
}
15
Using is:
JavaScript
1
2
1
let hasEnabled = treeHasEnabledNode()(this.tree);
2
How to return result not calling outher functon (this.tree)?
Advertisement
Answer
You can go about this a few ways. The most simple one would probably be to invoke the internal function within the outer function, and return the result:
JavaScript
1
13
13
1
function treeHasEnabledNode(node) {
2
let enabled = false;
3
4
function isNodeEnabled(node) {
5
// do whatever. for example:
6
return enabled
7
}
8
9
return isNodeEnabled(node);
10
}
11
12
const node = {};
13
console.log(treeHasEnabledNode(node));
However, as @sledetman mentioned in the comments below your question, the code snippet you provided does not “return a boolean if tree has at least one enabled value”.