1 参数函数没有参数,按照正常流程执行

demo:

function fun1(someFun){

console.log('this is from fun1');

someFun();

}

function fun2(){

console.log('this is from fun2');

}

fun1(fun2)

函数作为参数进行传递

 

 

 1 参数函数带参数,参数函数直接就执行了,在主函数中再次调用就会undifined

demo:

function fun1(someFun){

console.log('this is from fun1');

someFun();

}

function fun2(num){

console.log('this is from fun2');

console.log(num);

}

fun1(fun2(100))
函数作为参数进行传递

 解决办法,直接把参数和函数名作为两个参数进行传递:

demo:

function fun1(someFun,somedata){

console.log('this is from fun1');

someFun(somedata);

}

function fun2(num){

console.log('this is from fun2');

console.log(num);

}
fun1(fun2,1000)

函数作为参数进行传递

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-02-15
  • 2021-07-08
  • 2021-12-28
  • 2022-02-18
猜你喜欢
  • 2022-12-23
  • 2021-05-30
  • 2021-05-30
  • 2021-08-26
  • 2022-12-23
  • 2021-11-08
  • 2021-11-19
相关资源
相似解决方案