Static `this` is undefined during recursive call
See original GitHub issueWhile using this
in a static class method to recursively call itself works in the browser, it does not work with compiled code:
static buildTagCache(node, idCache) {
if (!node || (node.nodeType !== 1)) return; // not an element node
var nn = node.nodeName;
if (nn === 'point' || nn === 'link') {
// do some stuff
}
// search down
util.do4children(node, n => {
this.buildTagCache(n, idCache);
});
}
has an undefined
this
at the child call.
_Originally posted by @ray007 in https://github.com/google/closure-compiler/issues/3553#issuecomment-590834733_
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Recursion and static variables [duplicate] - Stack Overflow
It is bad style to call the main function recursively in C, and it has undefined behavior in C++. Ignoring the fact that...
Read more >V1092. Recursive function call during the static/thread_local ...
V1092. Recursive function call during the static/thread_local variable initialization might occur. This may lead to undefined behavior.
Read more >Why Recursive Function returns "Undefined"? - GeeksforGeeks
In simple words, recursion is when a function is calling itself directly or indirectly. It is used to break down the problem into...
Read more >Why isn't static variable recommended to be use in recursive ...
I don't know WHY someone would recommend not to use static variables in recursion. But, if a recursive function returns local static data...
Read more >Variable scope - Manual - PHP
Static variables also provide one way to deal with recursive functions. A recursive function is one which calls itself. Care must be taken...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
If it fixes the bug I’d probably use the flag. But it feels funny to require a flag to avoid a class of bugs…
Thanks for point this out! This is a compiler option that would solve this, as @brad4d mentioned in the other thread, but it hasn’t be exposed as a flag yet. There’s a field in
CompilerOptions
calledassumeStaticInheritanceRequired
that just needs to be settrue
.This option hasn’t been exposed mostly because it can really hurt
RemoveUnusedCode
, and also to try to reduce the number of compiler flags. However, issues like this may prioritize creating the flag.Would you use such a flag?