Javascript 使用来自两个不同函数的参数调用函数
我需要一些代码布局方面的帮助。 我可以用Javascript 使用来自两个不同函数的参数调用函数,javascript,function,arguments,nested-function,Javascript,Function,Arguments,Nested Function,我需要一些代码布局方面的帮助。 我可以用function1和function2中的参数调用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 a
function1
和function2
中的参数调用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
}
您可以在公共函数的作用域中创建两个变量并检查它们。大概是这样的:
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);
}
您可以使用Promise api来处理异步函数 此外,不能以数字开头命名函数
const element=document.querySelector(“#单击”)
功能一(arg){
返回新承诺((解决、拒绝)=>{
//获取该文件并将resolve函数传递给它
获取文件(解析)
})
}
功能二(arg){
返回新承诺((解决、拒绝)=>{
//将您的活动约束在承诺内
元素。addEventListener('click',e=>{
解析(arg)
console.log('单击已解决')
},错)
})
}
//承诺解决后的回调
函数三([arg1,arg2]){
console.log('calc resolved',arg1+arg2)
}
我保证([
一(1),
两(2)
]).然后(三)
//忽略这一点
函数getTheFile(解析){
//异步获取文件
设置超时(()=>{
//从文件中获取某些内容并将其传递给解析
决议(1)
console.log('文件已解析')
}, 250)
}
单击
问题是我想用两个参数调用函数fc3。一个来自fnc1,一个来自fnc2。我明白这是不可能的,我目前的代码,但我不知道如何解决它。希望这是明智的,谢谢你,我将研究api。这些不是真名,只是地名而已:)
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() )