回调函数:就是将函数作为参数,传递给另一个函数
场景:一般在异步调用中使用

作用:
1.将函数内部的值返回到外部
2.取代了return语句

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. </head>
  8. <body>
  9. <script>
  10. /*
  11. 回调函数:就是将函数作为参数,传递给另一个函数
  12. 场景:一般在异步调用中使用
  13. 作用:
  14. 1.将函数内部的值返回到外部
  15. 2.取代了return语句
  16. */
  17. var show = function(res){
  18. console.log(res);
  19. }
  20. function go(callback){
  21. var a = 10;
  22. callback(a);
  23. }
  24. go(show);
  25. </script>
  26. </body>
  27. </html>

例一

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        function go(callback){
            var a =10;
            callback(a);
        }
        go(function(res){
            console.log(res)
        })

        /*
        callback = function(res){
            console.log(res)
        }
        */
    </script>
</body>
</html>

Tips回到函数return是没用的,回调函数是函数的参数

function show(callback){
var a = 10;
callback(a);
}
var b = show(res=>{
return res;
})  //callback
console.log(b);