2016-12-01 55 views
-1

我需要某些代码的布局帮助。 我可以拨打function3来自function1function2的参数吗? 我不能让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 
} 

回答

0

您可以在常用函数的作用域中创建两个变量并检查它们。例如:

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); 
} 
+0

的事情是我想调用函数FC3用两个参数。一个来自fnc1,一个来自fnc2。我知道这是不可能与我目前的代码,但我不知道如何解决它。希望这使得sens –

0

您可以使用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>

+0

谢谢我将看看api。那些不是真正的名字只是地方hoolders :) –

+0

@williamganrot它现在的作品 – synthet1c

0
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()) 
相关问题