You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
test=->dynamic_scoped_function=->console.log("variable function value: #{variable_function()}")
variable_function=->"not in nested function"nested_function=->variable_function=->"in nested function"nested_caller=->console.log("calling dynamic scoped function from nested caller...")
dynamic_scoped_function()
{nested_caller}
{dynamic_scoped_function, nested_function}
# variable function value: not in nested functiontest().dynamic_scoped_function()
# calling dynamic scoped function from nested caller...# variable function value: in nested functiontest().nested_function().nested_caller()
This behavior is awkward in my opinion, because when I was writing dynamic_scoped_function, I assumed that variable_function would always be resolved to the outer one. In the original version of my buggy code where I found this problem, the inner variable_function has nothing to do with dynamic_scoped_function. They just happen to have the same name.
This unexpected situation could be eliminated if I add var on both variable_function's definition. Could the compiler act in this way, too?
The text was updated successfully, but these errors were encountered:
This unexpected situation could be eliminated if I add var on both variable_function's definition. Could the compiler act in this way, too?
CoffeeScript uses implicit scope. If we did that, we'd make it impossible to mutate a variable.
There were discussion to have an assignment operator for outer scope (the fork livescript uses :=) though.
This behavior is awkward in my opinion, because when I was writing
dynamic_scoped_function
, I assumed thatvariable_function
would always be resolved to the outer one. In the original version of my buggy code where I found this problem, the innervariable_function
has nothing to do withdynamic_scoped_function
. They just happen to have the same name.This unexpected situation could be eliminated if I add
var
on bothvariable_function
's definition. Could the compiler act in this way, too?The text was updated successfully, but these errors were encountered: