テスト用に次のコードがあります。
// Generated by CoffeeScript 2.2.3
(function() {
var t, test;
test = function() {
var dynamic_scoped_function, nested_function, vairable_function;
dynamic_scoped_function = function() {
return console.log(`variable function value: ${vairable_function()}`);
};
vairable_function = function() {
return "not in nested function";
};
nested_function = function() {
var nested_caller;
vairable_function = function() {
return "in nested function";
};
nested_caller = function() {
console.log("calling dynamic scoped function from nested...");
return dynamic_scoped_function();
};
return {
vairable_function,
nested_caller
};
};
return {
dynamic_scoped_function,
nested_function
};
};
t = test();
t.dynamic_scoped_function();
t.nested_function().nested_caller();
}).call(this);
node.jsで実行したときの結果は
variable function value: not in nested function
calling dynamic scoped function from nested...
variable function value: in nested function
で名前variable_function
が解決されるとdynamic_scoped_function
、呼び出し元のスタックに依存するようですが、期待どおりに外側のスタックに静的に解決されません。
dynamic_scoped_function
私の意見では、どこで呼び出されるかを予測できないため、この動作はばかげています。この言語はそのように動作するように設計されていますか? それとも、単に何かを誤解しただけですか?
ご協力いただきありがとうございます。