是否可以在javascript函数参数列表中调用多个函数
Is it possible to call more than one function within a javascript function parameter list
我正在学习javascript回调或更高阶的函数。请帮助我理解以下函数中的错误。我正在尝试调用多个函数作为回调函数。这是不允许的吗?
我的代码是:
var firstFunction = function(item1){
console.log("calling firstFunction");
console.log(item1);
}
var secondFunction = function(item2, f1){
console.log("calling secondFunction");
f1(item2);
}
//secondFunction("first", firstFunction);
var thirdFunction = function(item3, f2,f1){
console.log("calling thirdFunction");
f2(item3);
}
thirdFunction("second", firstFunction, secondFunction);
您所做的是正确的,但您必须在第三个函数中通过f1作为回调
var thirdFunction = function(item3, f2,f1){
console.log("calling thirdFunction");
f2(item3, f1);
}
您的代码还可以,回调深度没有限制,但会出现堆栈溢出错误。
在您的代码中,在脚本停止后,您将在第三个函数中执行firstFunction。我想你想在thirdFunction中执行secondFunction,然后在第二个函数中执行第一个:
function firstFunction (item){
console.log("calling firstFunction");
console.log(item);
}
function secondFunction (item, f1){
console.log("calling secondFunction");
f1(item);
}
function thirdFunction (item, f1,f2){
console.log("calling thirdFunction");
f2(item,f1);
}
thirdFunction("second", firstFunction, secondFunction);
该代码将以所描述的方式工作。
请看一下这个脚本
var firstFunction = function(item1, f2) {
document.body.innerHTML += item1 + " | This is firstFunction <br/> ";
//console.log("calling firstFunction");
f2(item1, secondFunction);
}
var secondFunction = function(item2, f1) {
document.body.innerHTML += item2 + " | This is secondFunction <br/> ";
}
var thirdFunction = function(item3, f1, f2) {
document.body.innerHTML += item3 + " | This is thirdFunction <br/> ";
f1(item3, f2);
}
thirdFunction("chain started third function", firstFunction, secondFunction);
<html>
<body></body>
</html>
相关文章:
- 是否可以将一个函数输入连接到另一个函数调用的文本
- 在输入字段上有两个函数调用,一个在Blur上,一个不在Angular中
- 如何在Javascript函数调用中循环变量
- Javascript:应为赋值或函数调用,但实际看到的却是表达式
- 如何远程检查JavaScript应用程序的函数调用堆栈
- javascript函数调用不起作用
- 为什么这个函数调用会破坏程序并导致未定义的变量
- 如何通过函数调用设置图像的src
- 从全局函数调用Ember控制器上的方法
- 为什么Jquery$.ajax在函数调用中触发所有statusCode,即使调用成功
- JavaScript函数调用(arg1)(arg2)
- 打印链接时,将javascript函数调用到链接中
- 在函数调用中封装数据除了隐藏数据之外还有什么优点
- 无法从JavaScript中的函数调用对象属性
- 对中的函数调用进行排序是回调的唯一方法
- 函数调用方法有什么用
- Javascript:JSHint:应为赋值或函数调用,但实际看到的却是表达式
- HTML5(Bootstrap)通过函数调用运行动画
- 函数调用不起作用
- 函数中的Javascript函数调用