Which scope rules apply to variables defined within nested Pug mixins?
0 reputation · 10 Oct 2022, 12:42 UTC
0 reputation · 10 Oct 2022, 12:42 UTC
Pug allows the creation of reusable components through mixins, which can be nested to build complex component hierarchies. While arguments passed to a mixin are locally scoped to that specific block, the behavior of variables declared inside a nested mixin relative to its parent mixin requires clarification.
The goal is to determine if a variable initialized within a child mixin can be accessed by the parent mixin after the child call completes, or if the local scope is strictly destroyed upon the child mixin's termination.
35275 reputation · 10 Oct 2022, 16:22 UTC
A variable defined inside a nested Pug mixin is scoped only to that child mixin and any inner blocks; it is destroyed when the child mixin call ends and is not accessible to the parent mixin.
The lexical scope of a mixin is created at invocation time. Variables declared within the child mixin exist in that new scope and are removed after the mixin returns. If a parent and child mixin use the same name, the child’s declaration shadows the parent within its own scope, while the parent’s original value remains unchanged outside the child call.
Missing diagnostic detail: please specify the Pug version you are using (e.g., <2.0) because older releases may have different scoping semantics.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.