レキシカルスコープの概念を理解しようとしています。私の知る限り、レキシカルスコープは逆方向には機能しません。以下の JavaScript コードでは、scope3() 関数で変数 'name' を宣言しています。しかし、scope1() および scope2() 関数で呼び出そうとしました。レキシカル スコープは逆方向には機能しないため、"name is undefined" を取得する必要がありましたが、空の文字列が返されます。誰かがこれを説明できますか?
var scope1 = function () {
// name should have been undefined but its printing empty string
console.log(name);
var scope2 = function () {
// name should have been undefined but its printing empty string
console.log(name);
var scope3 = function () {
var name = 'Todd'; // locally scoped
};
};
scope2();
};
scope1();