javascript中的回调函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<!DOCTYPE html>
<html>
 
    <head>
        <meta charset="UTF-8">
        <title>回调函数使用</title>
    </head>
 
    <body>
        <!--一。基本用法-->
        <script language="javascript" type="text/javascript">
            //回调函数基本用法:
            //      function dosomeThing(callback){
            //          callback("我是","回调",'函数');
            //      };
            //      function foo(a,b,c){
            //          alert(a+b+c);
            //      };
            //      dosomeThing(foo);
 
            //匿名函数中使用回调函数:
            //          function dosomeThing(domsg, callback) {
            //              alert(domsg);
            //              if(typeof callback == "function") {
            //                  callback("回调函数中的","参数");
            //              };
            //          };
            //          dosomeThing("匿名函数", function(a,b) {
            //              alert("此处是匿名函数中使用回调函数;"+a+b);
            //             
            //          });
        </script>
 
        <!--二。高级用法-->
        <!--<script language="javascript" type="text/javascript">
            function Thing(name) {
                this.name = name;
            };
                        Thing.prototype.doSomething = function(callback) {
                            callback.call(this);
                        };
            function foo() {
                console.log(this.name);
            };
            var t = new Thing("Joe");
            t.doSomething(foo);
        </script>-->
        <!--传参数-->
        <!--<script type="text/javascript">
            function Thing(name){
                this.name=name;
            };
            Thing.prototype.doSomething=function(callback,parameterss){
                callback.call(this,parameterss);
            };
            function foo(parameterss){
                console.log(parameterss+" "+ this.name);
            };
            var t=new Thing("Joe");
            t.doSomething(foo,"Hi");
        </script>-->
        <!--使用 javascript 的 apply 传参数-->
        <script type="text/javascript">
            function Thing(name) {
                this.name = name;
            };
            Thing.prototype.doSomething = function(callback) {
                callback.apply(this, ['Hi', 3, 2, 1]);
            };
 
            function foo(parameterss, three, two, one) {
                console.log(parameterss + " " + this.name + " " + three + two + " " + one); //输出:Hi Joe321
            };
            var t = new Thing("Joe");
            t.doSomething(foo);
        </script>
    </body>
 
</html>

  

posted @   生如逆旅,一苇以航  阅读(196)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术
点击右上角即可分享
微信分享提示