【问题标题】:Call a variable from inside a function into another function [closed]从函数内部调用变量到另一个函数[关闭]
【发布时间】:2020-10-16 03:31:56
【问题描述】:

我正在尝试将函数内部的变量调用到另一个函数中。我在节点 js 上运行代码,但它不工作。同时,当我在 jsfiddle 上尝试代码时,它运行正常。有什么建议吗?谢谢

var array = [];
 
function first(){  
     
array.push(3);
    
}

Function second(){console.log(array);}

first();
second();

【问题讨论】:

    标签: javascript node.js function global-variables local-variables


    【解决方案1】:

    您需要将功能更改为仅功能。 function 关键字区分大小写。

    请看这里:https://jsfiddle.net/qw7or2cg/1/

    var array = [];
     
    function first(){  
         
    array.push(3);
        
    }
    
    function second(){console.log(array);}
    
    first();
    second();
    

    【讨论】:

    • 欢迎 :)
    【解决方案2】:

    如果这是您在 Node 中使用的代码,请记住 Javascript 区分大小写,您编写的是“Function”而不是“function”。 试试:

    var array = []
    function first() {
      array.push(3)
    }
    function second() {
      console.log(array)
    }
    first()
    second()
    

    【讨论】:

      猜你喜欢
      • 2019-07-03
      • 2012-04-25
      • 1970-01-01
      • 2018-04-29
      • 2017-11-04
      • 2018-06-12
      • 2016-01-04
      • 2015-07-04
      相关资源
      最近更新 更多