我试图理解词法范围的概念。据我所知,词法范围不能向后工作。在下面的 javascript 代码中,我在 scope3() 函数中声明了变量“名称”。但我试图在 scope1() 和 scope2() 函数中调用它。由于词法范围不能向后工作,我应该得到“名称未定义”,但它返回空字符串。有人可以解释一下吗?
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();