question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Static `this` is undefined during recursive call

See original GitHub issue

While 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:open
  • Created 4 years ago
  • Reactions:1
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
ray007commented, Feb 28, 2020

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…

0reactions
nreid260commented, Feb 27, 2020

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 called assumeStaticInheritanceRequired that just needs to be set true.

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?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found