【问题标题】:Call a function with arguments from two diffrent functions使用来自两个不同函数的参数调用函数
【发布时间】:2017-04-16 08:13:48
【问题描述】:

我需要一些代码布局方面的帮助。 我可以用来自function1function2 的参数以某种方式调用function3 吗? 我不能让function2 成为一个嵌套函数,因为它是由onclick 激活的。

谢谢!

function 1(){
//This function activates when a file is imported and calculates one variable from an imported document.
}
function 2(){
//The function activates from an "onclick()" this function calculates another variable.  
}
function 3(){
//calculation of the two variables
}

【问题讨论】:

    标签: javascript function arguments nested-function


    【解决方案1】:

    您可以在常用函数的范围内创建两个变量并检查它们。像这样的:

    var firstVar = null;
    var secondVar = null;
    
    function fnc1(){
    //This function activates when a file is imported and calculates one variable from an imported document.
        firstVar = importedVariable;
        if(secVar){
            fnc3();
        }
    }
    function fnc2(){
    //The function activates from an "onclick()" this function calculates another variable.  
        secondVar = anotherVariable;
        if(firstVar){
            fnc3();
        }
    
    }
    function fnc3(){
    //calculation of the two variables
        alert(firstVar + secondVar);
    }
    

    【讨论】:

    • 问题是我想用两个参数调用函数 fc3。一个来自 fnc1,一个来自 fnc2。我知道这对于我当前的代码是不可能的,但我不知道如何解决它。希望这有意义
    【解决方案2】:

    您可以使用 Promise api 来处理异步函数。

    此外,您不能命名以数字开头的函数。

    const element = document.querySelector('#click')
    
    function one(arg) {
      return new Promise((resolve, reject) => {
        // get the file and pass the resolve function to it
        getTheFile(resolve)
      })
    }
    function two(arg) {
      return new Promise((resolve, reject) => {
        // bind your event inside the promise
        element.addEventListener('click', e => {         
          resolve(arg)
          console.log('click resolved')
        }, false)
      })
    }
    
    // callback when promises are resolved
    function three([arg1, arg2]) {
      console.log('calc resolved', arg1 + arg2)
    }
    
    
    Promise.all([
      one(1),
      two(2)
    ]).then(three)
    
    // ignore this
    function getTheFile(resolve) {
      // get the file asynchronously
      setTimeout(() => {
        // get something from the file and pass it to resolve
        resolve(1)
        console.log('file resolved')
      }, 250)
    }
    <button id="click">Click</button>

    【讨论】:

    • 谢谢,我会研究一下api。那些不是真实姓名只是放置持有者:)
    【解决方案3】:
    function Handler() {
        this.foo = function(v) { this.a = v }
        this.bar = function(v) { this.b = v }
        this.baz = function() { return this.a + this.b }
    }
    
    h1 = new Handler()
    h2 = new Handler()
    h3 = new Handler()
    
    h1.foo( 5 )
    h1.bar( 6 )
    
    h2.foo( 1 )
    h2.bar( 2 )
    
    h3.foo( 10 )
    h3.bar( 20 )
    
    print( h1.baz() )
    print( h2.baz() )
    print( h3.baz() )
    

    【讨论】:

      猜你喜欢
      • 2016-04-18
      • 2012-07-28
      • 2014-03-23
      • 2020-01-25
      • 1970-01-01
      • 1970-01-01
      • 2015-11-18
      • 2019-11-17
      • 2018-08-15
      相关资源
      最近更新 更多