回调函数与在另一个函数内部调用的函数之间的区别
The difference between callback and just a function being called inside of another function
有人可以教我:
- 作为参数传递的函数有什么区别
callback function
和一个在另一个函数内部调用的函数
调用某些代码后的函数?
- 什么时候应该使用每个场景?
我应该什么时候使用每个场景?
请看我下面的简单例子。感谢您分享任何帮助。
//func with callback passed as argument
function func1(name, fc1){
fc1(name);
}
const printIt = (name)=>{
console.log(name);
};
func1("samson", printIt);
//------------------------//
//func with a function called upon initial function being called.
function func2(name1){
//some code is called then printIt func is initialized
printIt(name1);
}
func2('sammy');
What's the difference between a function being passed as an argument callback function and a function being called inside of another function after some code is called?
这与您传递参数而不是使用硬编码值的任何时候都一样。
您可以拥有一些一致的逻辑,并根据您传递的值添加行为差异。
这很简单:
function add_two_numbers() { return 1 + 2; }
这是灵活的:
function add_two_numbers(a, b) { return a + b; }
When should I use each scenario?
当您想要固定或可变行为时。
有人可以教我:
- 作为参数传递的函数有什么区别
callback function
和一个在另一个函数内部调用的函数 调用某些代码后的函数? - 什么时候应该使用每个场景?
我应该什么时候使用每个场景?
请看我下面的简单例子。感谢您分享任何帮助。
//func with callback passed as argument
function func1(name, fc1){
fc1(name);
}
const printIt = (name)=>{
console.log(name);
};
func1("samson", printIt);
//------------------------//
//func with a function called upon initial function being called.
function func2(name1){
//some code is called then printIt func is initialized
printIt(name1);
}
func2('sammy');
What's the difference between a function being passed as an argument callback function and a function being called inside of another function after some code is called?
这与您传递参数而不是使用硬编码值的任何时候都一样。
您可以拥有一些一致的逻辑,并根据您传递的值添加行为差异。
这很简单:
function add_two_numbers() { return 1 + 2; }
这是灵活的:
function add_two_numbers(a, b) { return a + b; }
When should I use each scenario?
当您想要固定或可变行为时。