我在学习JS,想知道,有没有办法通过不返回foo()返回函数bar()或buz()?例如,如果我想返回函数bar();得到2,我能做到吗?它会像foo()。bar(); ?在JavaScript中嵌套范围调用内部函数
// Nested Scopes
function foo() { // can only access itself
var a = 1;
function bar() { // access to foo(), not buz()
var b = 2;
function buz() { // access to bar() and foo()
var c = 3;
console.log(a, b, c); // 1, 2, 3
}
buz();
console.log(a, b); // 1, 2
}
bar();
console.log(a); // 1
}
foo(); // 1
你是什么意思的“没有返回foo()”?你的意思是没有'console.log(a);'执行? – 4castle
我一直试图做@Xufox回答的。 –