Passing a local variable from one function to another

后端 未结 3 512
独厮守ぢ
独厮守ぢ 2021-01-30 09:29

I am a beginner in JavaScript and wanted to ask the following: I have two simple functions and was wondering if there is any way to pass a variable value from one function to an

相关标签:
3条回答
  • 2021-01-30 10:06

    Adding to @pranay-rana's list:

    Third way is:

    function passFromValue(){
        var x = 15;
        return x;  
    }
    function passToValue() {
        var y = passFromValue();
        console.log(y);//15
    }
    passToValue(); 
    
    0 讨论(0)
  • 2021-01-30 10:09

    First way is

    function function1()
    {
      var variable1=12;
      function2(variable1);
    }
    
    function function2(val)
    {
      var variableOfFunction1 = val;
    

    // Then you will have to use this function for the variable1 so it doesn't really help much unless that's what you want to do. }

    Second way is

    var globalVariable;
    function function1()
    {
      globalVariable=12;
      function2();
    }
    
    function function2()
    {
      var local = globalVariable;
    }
    
    0 讨论(0)
  • 2021-01-30 10:11

    You can very easily use this to re-use the value of the variable in another function.

    // Use this in source window.var1= oEvent.getSource().getBindingContext();

    // Get value of var1 in destination var var2= window.var1;

    0 讨论(0)
提交回复
热议问题