我做從nodeschool.io的JavaScript的做法,我是給這個代碼:JavaScript編程作用域鏈實踐
var a = 1, b = 2, c = 3;
(function firstFunction(){
\t var b = 5, c = 6;
\t (function secondFunction(){
\t \t var b = 8;
\t \t (function thirdFunction(){
\t \t \t var a = 7, c = 9;
\t \t \t (function fourthFunction(){
\t \t \t \t var a = 1, c = 8;
\t \t \t })();
\t \t })();
\t })();
})();
我不能夠訪問這裏面,所以我嘗試任何事情它是這樣的:
var a = 1, b = 2, c = 3;
var start = (function firstFunction(){
\t var b = 5, c = 6;
\t var secondFunction = (function secondFunction(){
\t \t var b = 8;
\t \t (function thirdFunction(){
\t \t \t var a = 7, c = 9;
\t \t \t (function fourthFunction(){
\t \t \t \t var a = 1, c = 8;
\t \t \t })();
\t \t })();
\t \t return {
\t \t \t secondFunction: secondFunction,
\t \t \t printB: function() {
\t \t \t \t return b;
\t \t \t }
\t \t }
\t })();
\t return {
\t \t printC: function() {
\t \t \t return c;
\t \t }
\t }
})();
console.log("a: " + a + ", b: " + start.secondFunction.printB() + ", c: " + start.printC());
我不知道我的方法是否正確,但我一直堅持這一整天。你會如何解決這個問題,爲什麼我的代碼不能進入secondFunction.printC?
方法是什麼?你想解決什麼問題? – Barmar
'secondFunction'不是從'start'返回的嗎? – guest271314
也爲什麼所有立即調用的函數,如果你將它們設置爲變量? – Damon