Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/434.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascripting范围链实践_Javascript - Fatal编程技术网

Javascripting范围链实践

Javascripting范围链实践,javascript,Javascript,我正在nodeschool.io中进行javascript实践,并获得以下代码: var a=1,b=2,c=3; (函数firstFunction(){ 变量b=5,c=6; (函数secondFunction(){ var b=8; (函数thirdFunction(){ 变量a=7,c=9; (函数第四函数){ 变量a=1,c=8; })(); })(); })(); })();试试这样的方法 var a = 1, b = 2, c = 3; var start = (function

我正在nodeschool.io中进行javascript实践,并获得以下代码:

var a=1,b=2,c=3;
(函数firstFunction(){
变量b=5,c=6;
(函数secondFunction(){
var b=8;
(函数thirdFunction(){
变量a=7,c=9;
(函数第四函数){
变量a=1,c=8;
})();
})();
})();

})();试试这样的方法

var a = 1, b = 2, c = 3;

var start = (function firstFunction(){
  var b = 5, c = 6;
  var obj = {};

  (function secondFunction(){
    var b = 8
    obj.secondFunction = function(){
        return b;
    };
    (function thirdFunction(){
        var a = 7, c = 9;
        obj.thirdFunction = function(){
            return c;
        };
        (function fourthFunction(){
            var a = 1, c = 8;
            obj.fourthFunction = function(){
                return c;
            };
        })();
    })();
  })();
  return obj
})();

console.log ( start.secondFunction() )

什么方法?您试图解决的问题是什么?
secondFunction
不是从
start
返回的?还有,如果您将所有立即调用的函数设置为变量,为什么会返回这些函数?请记住,我在这里学习。所以在webstorm中,当我键入start时。我看到我可以访问secondFunction和printB(),所以我认为我做得对。例如start.printC()确实返回它。@Barmar我正在学习javascript。我的目标是返回:a:1,b:8,c:6,b在secondFunction中,所以我只是想公开它;在firstFunction中,可以使用console.log返回这里的所有内容(“a:+a+”,b:+start.secondFunction()+”,c:+start.firstFunction());你能解释一下你是怎么做到的吗?我需要有什么不同的想法?